I'm working on a linux machine through SSH (Putty). I need to leave a process running during the night, so I thought I could do that by starting the process in background (with an ampersand at the end of the command) and redirecting stdout to a file. To my surprise, that doesn't work. As soon as I close the Putty window, the process is stopped.
How can I prevent that from happening??
When the session is closed the process receives the SIGHUP signal which it is apparently not catching. You can use the
nohup
command when launching the process or the bash built-in commanddisown -h
after starting the process to prevent this from happening:As others have noted, to run a process in the background so that you can disconnect from your SSH session, you need to have the background process properly disassociate itself from its controlling terminal - which is the pseudo-tty that the SSH session uses.
You can find information about daemonizing processes in books such as Stevens' "Advanced Network Program, Vol 1, 3rd Edn" or Rochkind's "Advanced Unix Programming".
I recently (in the last couple of years) had to deal with a recalcitrant program that did not daemonize itself properly. I ended up dealing with that by creating a generic daemonizing program - similar to nohup but with more controls available.
The double-dash is optional on systems not using the GNU getopt() function; it is necessary (or you have to specify POSIXLY_CORRECT in the environment) on Linux etc. Since double-dash works everywhere, it is best to use it.
You can still contact me (firstname dot lastname at gmail dot com) if you want the source for
daemonize
.However, the code is now (finally) available on GitHub in my SOQ (Stack Overflow Questions) repository as file
daemonize-1.10.tgz
in the packages sub-directory.Accepted answer suggest using nohup. I would rather suggest using pm2. Using pm2 over nohup has many advantages, like keeping the application alive, maintain log files for application and lot more other features. For more detail check this out.
To install pm2 you need to download npm. For Debian based system
and for Redhat
Or you can follow these instruction. After installing npm use it to install pm2
Once its done you can start your application by
For process monitoring use following commands:
Manage processes using either app name or process id or manage all processes together:
Log files can be found in
Binary executable files can also be run using pm2. You have to made a change into the jason file. Change the
"exec_interpreter" : "node"
, to"exec_interpreter" : "none".
(see the attributes section).Compiling above code
and run it with np2 in the background
I would recommend using GNU Screen. It allows you to disconnect from the server while all of your processes continue to run. I don't know how I lived without it before I knew it existed.
Use screen. It is very simple to use and works like vnc for terminals. http://www.bangmoney.org/presentations/screen.html
I used screen command. This link has detail as to how to do this
https://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/#starting