Keeping a process alive after ending SSH session
Typical scenario:
- SSH to a server
- Start a long-running process eg. webserver, utility script
- End SSH session
- Long-running process gets killed
Ending an SSH session usually kills all processes started during the session.
In order to keep the process alive even after the session is ended, there are a few methods:
nohup
The easiest method is to just start your process with nohup
which causes the process to ignore SIGHUP
, the hangup signal, fired to processes when the SSH session ends.
nohup ./script.sh &
disown
If your process is already running, you can suspend it using Ctrl-Z
then
bg
disown
to resume running the suspended task in the background, and disown
to remove ownership from the SSH session which allows the task to keep running.
screen
screen
is commonly used for managing terminal sessions; it includes a “detached” mode which we can use to detach a session from the SSH session.
screen -S session-name
will launch a new screen where you can start your process.
Ctrl-A Ctrl-D
will detach the screen and you can end the SSH session after this. If you want to reattach
screen -r session-name