Hack33.Keep Processes Running After a Shell Exits


Hack 33. Keep Processes Running After a Shell Exits

Process-control commands such as nohup and disown make it easy for you to start long-running processes and keep them running even after you log out.

Suppose you're running a troubleshooting or monitoring tool on your server or compiling a very large program, and the process is going to need to run for hours, days, or longer. What if you need that process to keep running even after you've logged out or if your shell session ends before you meant it to? You can make this happen with the nohup and disown commands.

When you run a shell session, all the processes you run at the command line are child processes of that shell. If you log out or your session crashes or otherwise ends unexpectedly, SIGHUP (signal to hang up) kill signals will be sent to its child processes to end them too.

You can get around this by telling the process(es) that you want kept alive to ignore SIGHUP signals. There are two ways to do this: by using the nohup ("no hangup") command to run the command in an environment where it will ignore certain termination signals or by using the bash shell's disown command to make a specified background job independent of the current shell.

4.6.1. Using nohup to Execute Commands

The nohup command provides a quick and easy mechanism for keeping a process running regardless of whether its parent process is still active. To take advantage of this capability, run your favorite command, preceded by the nohup command:

 $ nohup  command   

This executes the specified command and keeps it running even if the parent session ends. If you don't redirect output from this process, both its output and error messages (stdout and stderr) will be sent to a file called nohup.out in the current directory. If this file can't be created there, it will be created in the home directory of the user that ran the command.

You can monitor what's being written to nohup.out using the tail command:

 $ tail f ~/nohup.out 

You can also explicitly direct the output of your command to a specified file. For example, the following command line runs the specified command in the background, sends its output to a file called my_test_output.txt in your home directory and continues running it even if the parent session ends:

 $ nohup  command > ~/my_test_output.txt &  

If you don't want to save the output of the specified command, you can discard it (and not create the nohup.out file) by redirecting output to /dev/null, the Linux bit-bucket:

 $ nohup  command  > /dev/null &  

This runs the command or program in the background, ignores its output by sending it to /dev/null, and continues running it even if the parent session ends.

If you've used nohup to keep a process running after its parent exits, there is no way to reconnect to that process if you subsequently want to shoot it down. However, nohup only protects the process from the SIGHUP signal. You can still terminate it manually using the big kill hammer, kill9 PID.


4.6.2. Using disown with Background Jobs

If you're using the bash shell, you can tell an existing process to ignore SIGHUPs by using the shell's disown built-in command:

 $ disown -h  jobnumber  

This tells a job already running in the background to keep running when its parent process shuts down. You can find its job number using the shell's jobs command. If you use the disown built-in's -h option, the running job won't be removed from the jobs table when you disown it, but it will keep running if the current shell ends. You can still reconnect to this process using the standard bash %job-number mechanism. If you use the disown built-in with no options, the running job will be removed from the jobs table: it will still continue running after you log out, but you won't be able to reconnect to it.

You can also use the disown command to keep all current background jobs running:

 $ disown -ar 

This tells all running jobs to keep running even if the current shell closes.

4.6.3. See Also

  • man bash

  • man1 nohup

Jon Fox



Linux Server Hacks (Vol. 2)
BSD Sockets Programming from a Multi-Language Perspective (Programming Series)
ISBN: N/A
EAN: 2147483647
Year: 2003
Pages: 162
Authors: M. Tim Jones

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net