views:

348

answers:

4

I would like to run an asynchronous program on a remote linux server indefinitely. This script doesn't output anything to the server itself(other than occasionally writing information to a mysql database). So far the only option I have been able to find is the nohup command:

nohup script_name &

From what I understand, nohup allows the command to run even after I log out of my SSH session while the '&' character lets the command run in the background. My question is simple: is this the best way to do what I would like? I am only trying to run a single script for long periods of time while occasionally stopping it to make updates.

Also, if nohup is indeed the best option, what is the proper way to terminate the script when I need to? There seems to be some disagreement over what is the best way to kill a nohup process.

Thanks

A: 

That is the simplest way to do it if you want to (or have to) avoid changing the script itself. If the script is always to be run like this, you can write a mini script containing the line you just typed and run that instead. (or use an alias, if appropriate)

To answer you second question:

 $ nohup ./test &
[3] 11789
 $ Sending output to nohup.out

$jobs
[1]-  Running                 emacs *h &
[3]+  Running                 nohup ./test &
$ kill %3
$ jobs
[1]-  Running                 emacs *h &
[3]+  Exit 143                nohup ./test

Ctrl+c works too, (sends a SIGINT) as does kill (sends a SIGTERM)

Chris Huang-Leaver
+2  A: 

If you can modify the script, then you can catch SIGHUP signals and avoid the need for nohup. In a bash script you would write:

trap " echo ignoring hup; " SIGHUP

You can employ the same technique to terminate the program: catch, say, a SIGUSR1 signal in a handler, set a flag and then gracefully exit from your main loop. This way you can send this signal of your choice to stop your program in a predictable way.

catwalk
+1  A: 

What you are basically asking is "How do I create a daemon process?" What you want to do is "daemonize", there are many examples of this floating around on the web. The process is basically that you fork(), the child creates a new session, the parent exits, the child duplicates and then closes open file handles to the controlling terminal (STDIN, STDOUT, STDERR).

There is a package available that will do all of this for you called python-daemon.

To perform graceful shutdowns, look at the signal library for creating a signal handler.

Also, searching the web for "python daemon" will bring up many reimplementations of the common C daemonizing process: http://code.activestate.com/recipes/66012/

charstar
A: 

There are some situations when you want to execute/start some scripts on a remote machine/server (which will terminate automatically) and disconnect from the server.

eg: A script running on a box which when executed 1) takes a model and copies it to a custer (remote server) 2) creates a script for running a simulation with the wodel and push it to server 3) starts the script on the server and disconnect 4) The duty of the script thus started is to run the simulation in the server and once completed (will take days to complete) copy the results back to client.

I would use the following command:

ssh remoteserver 'nohup /path/to/script `</dev/null` >nohup.out 2>&1 &'

eg:

echo '#!/bin/bash  
rm -rf statuslist  
mkdir statuslist  
chmod u+x ~/monitor/concat.sh  
chmod u+x ~/monitor/script.sh  
nohup ./monitor/concat.sh &  
' > script.sh 

chmod u+x script.sh

rsync -azvp script.sh remotehost:/tmp

ssh remoteshot '/tmp/script.sh `</dev/null` >nohup.out 2>&1 &'

Hope this helps ;-)

Jayan