Keep the running processes alive when disconneting the remote connection
I have a bunch of processes on a server initiated by ssh from my own machine. It is now about three hours that my machine's Ubuntu has got hung and I do not see any other options but restart. Is there any way to keep the processes on the server alive? I have the root access to both my machine and server.
thanks in advance
1 Answer
There are several ways to achieve this:
nohup
use nohup to run the command so that the process is immune to hang ups (log out).
For example: nohup command > /path/to/log 2>&1 &
NOTE: the above runs command immune to
SIGHUPin the background and redirectsstdout/stderrto a file specified.
setsid and disown
Use setsid and disown Basically this allows the process to run as a new session (init / PID 1 as its parent) so that user logout doesn't affect it. You can read the manual for more information.
Recommended approach
Use tmux or screen
Run command/apps in a tmux or screen session. I personally prefer tmux. Technically it runs a server on the host (remote server), as long as the daemon does NOT die, all session information will be preserved.
NOTE: if you get disconnected, just ssh to the server again and run
tmux attach -t <target-session>to get back to the sessions. Usetmux lsto list available sessions.