Keeping a process running after PuTTY or terminal has been closed

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.


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 https://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.)


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 `cat node-instance.pid`

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