Unix is multitasking, meaning that you can run multiple programs simultaneously. You can also run multiple instances of the same program, unlike Microsoft Windows or the Macintosh.
Append an ampersand (&) to the end of the command line to run a program in the background. You'll get a message from the shell giving the "job number" of the background process, and then the shell prompt will reappear.
(~) 66% grep error /var/log/messages* & [1] 14531 (~) 67% /var/log/messages.3:Sep 1 06:09:44 pesto kernel: Using exception 16 error reporting. [1] Done grep error /var/log/messages*
The command here was a grep to search for the string "error" in the system message log. The system replied with a cryptic message giving the job number in square brackets, followed by the process ID. Then the command prompt reappeared. Sometime later we see some of the program's output -- a matching line from /var/log/messages.3. Some time after that, grep finishes its search. The shell prints out the job number, and a message indicating that the program is done.
Usually you'll want to redirect the program's standard output and/or error in order to prevent the background program's output from interfering with other things you're doing. If you try to background a program that needs to read from standard input, this will happen:
(~) 78% mail -s "what's up doc?" lstein & [1] 14647 (~) 79% [1] + Suspended (tty input) mail -s what's up doc? lstein (~) 79%
The mail command expects to read data from standard input. It can't read from standard input while it's in the background, so it is suspended until it is brought into the foreground (see below for how to do this). A better approach would be to redirect its standard input from a file:
(~) 79% mail -s "what's up doc?" lstein <message.txt & [2] 14654 (~) 80% [2] Done mail -s what's up doc? lstein < message.txt
A program is either in the foreground or the background. It can only read from the terminal while it is in the foreground, but it can write to the terminal in either state (which is sometimes convenient and sometimes annoying).
If a program is in the foreground, you can suspend it and go off to do something else by typing control-Z (^Z):
(~) 80% tail -f /var/log/syslog Sep 5 17:32:22 pesto xntpd[113]: sendto(192.168.2.1): Network is unreachable Sep 5 17:32:25 pesto dhclient: receive_packet failed on eth0: Network is down Sep 5 17:53:06 pesto kernel: MIDI Loopback device driver Sep 5 17:57:30 pesto dhclient: receive_packet failed on eth0: Network is down Sep 5 17:57:32 pesto xntpd[113]: sendto(128.59.16.20): Network is unreachable ^Z Suspended (~) 81%
Once suspended, a program does nothing until it is resumed or killed. You can review the jobs you have running with the shell's jobs command:
(~) 81% jobs [1] - Suspended (tty input) mail -s what's up doc? lstein [2] + Suspended tail -f /var/log/syslog
The "tty input" comment on job #1 is to remind you that the program is suspended because it's waiting for input from the terminal.
You can bring a job into the foreground with the fg command:
(~) 82% fg %2 tail -f /var/log/syslog Sep 5 17:57:50 pesto xntpd[113]: sendto(143.48.7.74): Network is unreachable Sep 5 17:58:09 pesto xntpd[113]: sendto(143.48.7.55): Network is unreachable Sep 5 17:58:17 pesto xntpd[113]: sendto(128.59.16.20): Network is unreachable ^Z Suspended
The "%2" in this example refers to the job number. I suspended it again soon after bringing it into the foreground.
You can run a suspended job in the background with the bg command:
(~) 85% bg %2 [2] tail -f /var/log/syslog &
If you try to background a program that is suspended waiting for the terminal, it will immediately suspend itself again:
(~) 86% bg %1 [1] mail -s what's up doc? lstein & (~) 87% (continue) [1] + Suspended (tty input) mail -s what's up doc? lstein
You'll have to bring it into the foreground, give it the input it wants, then suspend and background it.
If a job is running in the foreground, you can kill it entirely by typing control-C (^C), fondly referred to as the "interrupt key".
If a job is running in the background, you can either bring it to the foreground and interrupt it, or use the kill command. Kill takes either the job number or the process ID:
(~) 87% kill %1 (~) 88% [1] Terminated mail -s what's up doc? lstein
Some programs can be hard to kill, because they detect and trap the interrupt key. You can usually kill these programs by passing kill the redundant-sounding -KILL switch. If you've ever seen the movie, Faster Pussycat, Kill Kill! you'll understand:
(~) 90% kill -KILL %2 (~) 91% [2] Killed tail -f /var/log/syslog
In addition to the job number, kill can be called with the process ID of the program. You can obtain the process IDs of all the programs you're running with the ps command:
(~) 98% ps PID TTY TIME CMD 8149 pts/2 00:00:00 tcsh 14647 pts/2 00:00:00 tail 14728 pts/2 00:00:00 mail 14729 pts/2 00:00:00 ps
The PID column gives the process ID. The TTY column gives the terminal name. TIME contains the CPU time that the command has used, and CMD gives the command name. Notice that the shell and ps itself are running, in addition to the tail and mail programs that we launched.
|
| Contents |
Next |