August 24th 2014

When I first set up a few web applications on my Digital Ocean instance I had a problem where my applications would stop working after a while. I would start the processes in an SSH session and while I was still connected to the server everything worked fine. However when the session would time out, the application would stop working.

I soon found out that a process started within an SSH session is tied to that session. This means that once the session ends, all processes started in that session are also terminated, much like a process started in a terminal window is stopped when the terminal window is closed.

There are several ways to get around this problem. For normal administration the best way is to persist the session using tmux or screen, like instructed here.

However if I'm setting up an Ansible playbook or a Bash script to start my application then it's easier to use the commands nohup or disown as follows:

# Option 1:
nohup command arg1 arg2

# Option 2:
command arg1 arg2 & disown

There is an important difference between the two though. Using disown the process will fail if it uses stdin or stdout, while nohup completely disconnects the process from the session. There is a detailed description of the differences on stackexchange.