Current Student Resources / ComputingWorking Offline
Running a Program While Logged Out
For security and courtesy reasons, you should avoid leaving a department computer unattended while logged in. Fortunately, there are several ways to follow this policy while still getting things done.
If you want to keep an entire shell (or multiple shells) and any child processes running even when logged out, then check out screen. But if you have currently running jobs or just want to start a job and log out without needing to monitor it, then there are two lightweight solutions:
- If your job is currently running, and you want to log out but leave it running in the background while you are gone, you can use disown to unbind it from the login session.
- If you are about to run a program but plan to log out during its execution, then you can use the nohup command.
Both methods work by removing the process as a child of your login shell and attaching it instead to init (the parent process) as a daemon; the process will now ignore your hangup signal and hence will keep running if you log out or exit the shell. Since the process will finish running in the background, make sure that you redirect any I/O streams used by the process if you care about their contents.
Try to avoid using disown or nohup on persistent or interactive programs, Matlab is a good example, since they don't terminate themselves and will continue to run in the background and use system resources until explicitly killed or until the machine is shut down.
Example
Suppose that you are running some FORTRAN code,
$ ./solvebigproblem
but now you want to go to lunch with some friends. Instead of locking the screen, simply put the process in the background by first suspending the currently running job
$ <Ctrl-Z>
then force the job to run in the background
$ bg
and finally
$ disown
If you have multiple jobs in the background already, you can view you list of jobs by running
$ jobs
disown each using
$ disown <jobid>
Now suppose that you about about to leave for the night, but you want to run your code overnight. You could disown to do this as well, or use nohup:
$ nohup solvebigproblem > output-file 2>&1 &
This starts the program in the background and ensures that it will continue running once you log out. Note that both the stdout and stderr streams are redirected to output-file, so that you can look at the program output in the morning.
When Using the C Shell
If you use csh or tcsh, be aware that
- disown is bash-specific
- the C shell has its own version of nohup, so make sure that you use the full path /usr/bin/nohup