Skip Navigation
 Search | Directories | Reference Tools
UW Home > Discover UW > IT Connect > Unix 

Job Control

In Unix, a job can be in one of three states. It is either in the foreground, in the background, or stopped. Most commands you type run foreground jobs. Once you have entered a command to run a job, you wait until the job is done and then you get another UNIX prompt. To run a job in the background, enter the job command followed by the & sign. This returns the Unix prompt to you and executes the job while still allowing you to interact with your terminal. Jobs may be switched from foreground to background and vice versa.

If you have a foreground job running and you type ^Z (control z) the job is stopped. Then if you type `bg' the job will start up again in the background. If you then type `fg' it will be in the foreground again. Don't worry - ^Z does NOT kill the job, it merely stops it temporarily. If you want to list the status of all stopped jobs and jobs you have running in the background, enter the command


Output produced by this command will look something like the following:

     [1]    Stopped              vi potato
     [3]  + Running              spudmasher
     [4]  + Stopped              a.out

If you want to kill spudmasher, which is job number 3, enter `kill %3'. If you try to log out while you have stopped jobs, the shell will warn you that there are suspended jobs. This warning is helpful, since it is possible that you may have stopped jobs you've forgotten about. If you don't care about any of the stopped jobs, just type logout a second time and they will all be killed.

When you have a list of stopped jobs, you can use the `bg' and `fg' commands in the same way as the `kill' command described above (i.e., enter either `bg %n' or `fg %n'). For example, `fg %3' will bring spudmasher into the foreground, `bg %4' will start a.out running in the background.

These job control facilities only apply to the current csh you are running. For example, the `jobs' command will not know about jobs you started in a different login session. To kill a job that was run within another login session, you need to use the more general command `ps' together with the `kill' command. Use `man' for details about these commands.

For more information about job control, refer to the C-Shell Manual pages in the UNIX User's Manual Reference Guide (identified as CSH). You can also see this entry online by typing `man csh.' Another source of information is the "Introduction to the C-Shell" article in the in the UNIX User's Manual Supplementary Documents volume.

man pages: csh(1), kill(1)