Background Jobs

My program's going to take a long time. How do I leave it running when I'm not logged in, and how can I find out if it's finished when I log in next?


Running a job in the background

A background job is a running program which does not send data to, or take input from, an xterm or console screen. It can stay running when the xterm it was started from is killed, and even when the user logs out. If you start a background job from a shell, you will get your shell prompt back almost straight away and so can carry on doing other things with that xterm. A foreground job is a running program which takes over the xterm so all input in that window goes to the program.

The default is to start jobs in the foreground. To start a job in the background, simply put an ampersand (&) at the very end of the line, for example:


torbernite% xterm &



This will give you a new xterm but leave the old one free. You can also move a foreground job into the background by pressing CTRL-z in the window (ie, hold down the Control key and press z) to suspend the job and then entering bg to continue the job in the background.

Alternatevely, you may wish to use screen to do this.

Redirecting input and output

Most programs that you'll want to run in the background will need to be given some kind of input, and will want somewhere to put their output. See the input and output redirection help to find out how to do this.

Monitoring background jobs and bringing a job back to the foreground

An example of bringing a process in the background to the foreground

csh@localhost% ssh fasthost
csh@fasthost% nice ~/programs/foo < ~/data/foo.dat >& ~/output/foo.out &
[1] 9999

(the job's running in the background)
csh@fasthost%

csh@fasthost% fg 1
nice ~/programs/foo < ~/data/foo.dat >& ~/scratch/foo.out

(the job's now running in the foreground)

The command fg 1 brings job 1 back to the foreground. When you log out of the machine the controlling TTY is lost, so you are unable to bring the job back to the foreground. You'll need to use the command ps to control the process once you've closed the controlling tty.

For example; I have left the program foo running (as above) in the background and I now want to log back into fasthost (1 day later) to check to see if the program is still running.


csh@localhost% ssh fasthost

csh@fasthost% ps x

   PID TTY      S           TIME CMD

  9999 ??       S N     23:21.03 ~/programs/foo

  1234 ttyp1    S        0:04.45 -csh (tcsh)

Doing a ps shows the program under the heading CMD is running. The ps command also shows that the TTY (controlling TTY=??) for the program has been lost because I have logged out after starting the program in the background.

After looking at the files which the program has created, it seems the program has completed it's job but has been poorly coded and has failed to exit cleanly. I want to get rid of it - or kill it.


csh@fasthost% kill 9999

csh@fasthost% ps x

   PID TTY      S           TIME CMD

  1234 ttyp1    S        0:04.45 -csh (tcsh)

  9999 ??       S N      0:00.03 ~/programs/foo

Oh no! It appears the program has got into a stubborn state where it doesn't recognise a TERM signal (the default signal generated by kill) so:


csh@fasthost% kill -9 9999

csh@fasthost% ps x

   PID TTY      S           TIME CMD

  1234 ttyp1    S        0:04.45 -csh (tcsh)

The kill -9 sends a KILL signal to the process which kills the process outright.

Please contact us with feedback and comments about this page. Last updated on 25 Mar 2022 15:43.