Department of Engineering

IT Services

Processes and Job Control

To run a command you just type its name, e.g. date.

If you start the command this way then you cannot use the terminal for anything else until it has finished. This is satisfactory provided your command is not going to take very long.

Another alternative is to start the command `in the background.' This sets the command going then prompts you again in case there is anything you want to do while the command is running. To start a command in the background you type an `&' at the end of the command. Try

sleep 1000 &

sleep is a command that does nothing for the number of seconds you specify, so what we have started is a command which sleeps in the background for 1000 seconds. This is not a very useful command but it could equally well be a long computation job.

You can find out what background jobs you have running using the jobs command. Try it now.

Suppose you want to bring your sleep job into the foreground now. If it was a real program it might need some input for instance. The way to do this is to type fg.

Notice that the prompt does not come back. Now that the job is in the foreground you can either kill it (forever) with C-c, or stop it with C-z (hold down CTRL and type z).

Stop the sleep command by typing C-z. You can now return it to running in the background with the command bg.

If you want to get rid of the sleep command now, bring it into the foreground using fg and then kill it with C-c.

Note that all your processes, whether in foreground or background, are likely to be killed when you log off. If you need to run long jobs then consult the online manual page for batch (or, if at CUED, look at the Grid Engine page).

To list the programs that you have running on the machine you're logged into, type ps -f. You'll get output rather like this

   UID        PID  PPID  C STIME TTY          TIME CMD
   tpl      19471 19470  1 11:44 pts/0    00:00:00 -bash
   tpl      19507 19471  0 11:44 pts/0    00:00:00 ps -f

You can kill one of these programs by using the kill command followed by the Process IDentity number (the number in the 2nd column). E.g.

    kill 19507

(or "kill -KILL 19507" to be sure). The top command lists the busiest programs on the system.