Job Control

As mentioned earlier, one of the distinctives of Unix is that it is designed to let multiple individuals run multiple programs on a single CPU simultaneously. To take advantage of this, and to juggle multiple tasks effectively, learn the following commands and syntax.

ps
The computer is doing more things than your realize. The command ps displays currently running processes; you can specify the detail desired. Examples:

The process ID number (PID) can be important. If you want to kill a program for some reason, and it is running in the foreground, you can usually do it by typing ^C (control-C). However, sometimes this doesn't succeed, or perhaps the program is running in the background (see below) where ^C can't get at it. In that case, you can find its process ID number and use kill. Examples: So, if your program myprog is running out of control and you can't stop it by pressing control-C, you can find its PID and kill it:
  simpson>  ps -ef | grep myprog
      smithj 17493     1  0 10:18:19 pts/4   0:41 /home/smithj/bin/myprog
  simpson>  kill -9 17493

&
Some processes can be launched which neither need further keyboard input nor produce text output. They can be run in the background, allowing you to type other commands while the background job keeps running. Jobs running in the background can continue to run even after you log off the computer and go home, assuming they don't require keyboard input or screen display; this is useful for when you want to run huge calculations on our systems.

Run commands in the background by typing an ampersand, &, at the end of the command. Examples:

What happens if you forget to type the &? You don't have to kill the program and retype the command; rather, you can stop it by typing ^Z (control-Z) to suspend the process, then type bg to set it running again in the background.

jobs
Typing the command jobs shows all the background jobs being run from the current window. If you want to restore any of them to foreground status, you type fg job#. (The job number is not the same as PID.)

simpson>  jobs
[1]  + Running                       netscape
[2]  - Running                       myprog
[3]  - Running                       mathematica
simpson>  fg 2
myprog

nice
When you run big numbercrunching programs in the background, the kind which run for hours or days or weeks, you should precede the command with the word ``nice''. Then the program will take less of the CPU when other users log in and run real-time jobs. Example: