Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

This question already has an answer here:

I'm running a node.js server off of a raspbian (Debian) machine, and I'd like to start and stop the server remotely. This for me means using putty to access the shell, except when I close out of the putty terminal or it times out my server goes down with it, because I just execute my server in the foreground.

So I'm wondering if there's a way to keep it going but still have a way to kill the process afterwards.

share|improve this question

marked as duplicate by Gilles, slm, Mat, phunehehe, manatwork Sep 6 '13 at 8:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
i'm assuming that you SSH into your server, then run your node program, which executes in the foreground? –  strugee Sep 5 '13 at 5:15
    
I'm not really familiar enough with Linux to answer that but it sounds correct –  Spencer Sep 5 '13 at 5:19
    
I'm just a little foggy on the foreground part –  Spencer Sep 5 '13 at 5:20
    
basically, on the terminal of your Pi, you type the command to start your server. it doesn't return you to a prompt, and instead spews logs from your server. that sound right? –  strugee Sep 5 '13 at 5:20
    
Yes when it runs it logs right into the terminal –  Spencer Sep 5 '13 at 5:35

4 Answers 4

up vote 8 down vote accepted

Your question was a little lacking in details, so I'm assuming that you mean that you typed the command to start your server on the console of your Pi, and it executed in the foreground.

If this is the case, you have five options, ordered by complexity to implement:

  1. Use @f-tussel's answer. Since you're new to GNU/Linux, the & symbol tells the shell that it should execute the process in the background and return you to the prompt immediately, instead of what it normally does (which is wait for the command to finish before returning you to the prompt). This is technically called forking the command to the background.

  2. Do what you did before, but do it in a screen process. Basically this entails installing screen (sudo apt-get install screen on your Debian system), and then sometime before you type the command to start your server, you execute screen. This opens a new shell that you can then reconnect to later, even if your PuTTY connection dies. So it will act as if you've never disconnected.

    If you're unfamiliar with screen, you may want to do some reading on Wikipedia and in the manpages. You can also accomplish this same thing with tmux.

  3. Use the forever node.js module. See http://stackoverflow.com/questions/4797050/how-to-run-process-as-background-and-never-die for where I got this.

  4. Put your server in a screen process in the background. This means that you'll create a new screen session in the background but never attach to it. And, instead of running a shell, the screen process will be running your server. Here's what you'll type:

    screen -d -m exec_your_server --put-args-here
    

    If you like, you can make this run at boot. Basically you need to put the screen command in the file /etc/rc.local or /etc/rc.d/rc.local, I forget which. If you run into trouble doing this, ask a new question.

    Again, you can do this with tmux too.

  5. Write a service script. Since you're on Debian and are new, you're presumably using the default init that Debian provides, which is System V Init. I've never looked at service files for System V Init, only systemd and a little Upstart, so I can't help you here. Ask a new question if you want to pursue this.

    This is the least "hacky" way, IMHO, and this is what you should consider doing if you're running your server long-term, as you can then manage it like other services on the system through commands like sudo service your_server stop, etc. Doing it this way will start your server at boot automatically, and you don't need screen because it also automatically happens in the background.

    It also automatically executes as root, which is dangerous - you should put logic in your server to drop the privileges that you have by becoming an unprivileged user that you have created specifically for the server. (This is in case the server gets compromised - imagine if someone could run things as root, through your server! Eugh. This question does an OK job of talking about this.)

share|improve this answer

I don't know about raspbian, but since it is derived from Debian, I assume nohup will be available too. Instead of running a process as,

$  proc &

try to use:

$ nohup proc &

The nohup will prevent the process from being terminated when the terminal disconnects. HTH.

share|improve this answer
    
Thanks Iv seen nohup as well in my searches is this just a typo? Or something different. Iv also seen use of the 'screen' command is hohup a better alternative? –  Spencer Sep 5 '13 at 5:32
    
@Spencer they both do different things. I'm writing an answer that explains screen, if it's useful. –  strugee Sep 5 '13 at 5:35
    
Thank you, yes anythings useful because ill probably use it in the future –  Spencer Sep 5 '13 at 5:38
    
@Spencer: yes, it was a typo in the first line (now corrected). Below it was spelled correctly. –  F. Tusell Sep 5 '13 at 7:22

Use supervisord if you're really serious.

Basic way of doing this to run your binary as:

$ nohup node app &
$ echo $! > node-instance.pid

Then, when you want to kill it,

$ kill `echo node-instance.pid`

But I would use supervisord. It allows you do a lot of fancy things.

share|improve this answer
    
What do you mean "really serious?" –  Spencer Sep 5 '13 at 12:51
    
@Spencer: A person is 'really serious' when one thinks that it would be better if there were a more standardized way of running daemon processes. –  onur güngör Sep 6 '13 at 5:40

If you're running bash a shell, you can alternatively use the disown command which will have the same result than the nohup command except it takes the first running program it finds. For example :

$ command &
$ disown

Or maybe you can create a script that forks your process and kills the father so that the child is inherited by init. That's what I do for scripts for example. It's not simpler but when you launch it, it automatically goes to background and doesn't terminate if you log off.

Another solution would be to create a service file and enable it so you can start, stop, restart or whatever with a simple command. (but I don't really know if debian is using systemd since I'm on archlinux)

share|improve this answer
    
Debian is not using systemd. Debian is using System V init, although you can install Upstart and systemd if you want, with some caveats. I can't find the question on U&L about problems with Upstart, but see wiki.debian.org/systemd#Known_Issues_and_Workarounds - it's basically the same if you try to use Upstart. –  strugee Sep 5 '13 at 21:24
    
this did the trick for me (ubuntu) –  aldo.roman.nurena Nov 24 at 10:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.