5.18. Job ControlConvenient multitasking is one of the best features of Linux, so it's important to be able to obtain a listing of your current processes and control their behavior. There are two utilities and one built-in command that allow you to do this:
The next few sections describe these facilities in more detail. 5.18.1. Process Status: psThe ps utility allows you to monitor the status of processes (Figure 5-13).
In the following example, I made use of the sleep utility to delay a simple echo statement, and placed the command in the background. I then executed the ps utility to obtain a list of my shell's associated processes. Each "sh" process was a Bash shell process; one of them was my login shell, and the other was the subshell created to execute the command group. $ (sleep 10; echo done) & ...delayed echo in background. 27387 ...the process ID number. $ ps ...obtain a process status list. PID TTY TIME CMD 27355 pts/3 00:00:01 sh ...the login shell. 27387 pts/3 00:00:00 sh ...the subshell. 27388 pts/3 00:00:00 sleep 10 ...the sleep. 27389 pts/3 00:00:00 ps ...the ps command itself! $ done ...the output from the background process. For the record, Figure 5-14 describes the sleep utility.
The meanings of the common column headings of ps output are given in Figure 5-15.
The S field encodes the process's state as described in Figure 5-16.
The meanings of most of these terms are described later in the book; only the R and S fields will make sense right now. Here's an example of some user-oriented output from ps: $ (sleep 10; echo done) & 27462 $ ps -f ...request user-oriented output. UID PID PPID C STIME TTY TIME CMD glass 24379 23708 0 16:41 pts0 00:00:00 ksh glass 24382 24379 0 16:41 pts0 00:00:00 sleep 10 glass 24803 24382 0 17:28 pts0 00:00:00 ps -f $ done ...output from previous command If you're interested in tracking the movements of other users on your system, try the -e and -f option of ps: $ ps -ef ...list all users' processes. UID PID PPID C STIME TTY TIME CMD root 1 0 0 Aug25 ? 00:00:02 init [5] root 2 1 0 Aug25 ? 00:00:00 [ksoftirqd/0] root 3 1 0 Aug25 ? 00:00:00 [events/0] root 4 1 0 Aug25 ? 00:00:01 [kblockd/0] root 5 1 0 Aug25 ? 00:00:00 [kapmd] root 7 1 0 Aug25 ? 00:00:00 [pdflush] root 8 1 0 Aug25 ? 00:00:05 [kswapd0] root 742 1 0 Aug25 ? 00:00:00 syslogd -m 0 root 750 1 0 Aug25 ? 00:00:00 klogd -2 daemon 965 1 0 Aug25 ? 00:00:00 /usr/sbin/atd root 1003 948 0 Aug25 ? 00:00:00 -:0 root 1078 1 0 Aug25 ? 00:00:00 crond glass 1362 1282 0 Aug25 ? 00:03:01 magicdev glass 1376 1 0 Aug25 ? 00:00:00 /usr/lib/gconfd glass 1463 1282 0 Aug25 ? 00:00:00 kwrapper glass 23708 23693 0 15:51 pts0 00:00:00 bash root 24379 23708 0 16:41 pts0 00:00:00 su root 24382 24379 0 16:41 pts0 00:00:00 bash root 24805 24382 0 17:28 pts0 00:00:00 ps -ef $ _ The Korn shell automatically terminates background processes when you log out, whereas the C and Bash shells allow them to continue. If you're using the Korn shell and you want to make a background process immune from this effect, use the nohup utility to protect it (Figure 5-17).
If you execute a command using nohup, log out, and then log back in again, you won't see the command on the output of a regular ps. This is because a process loses its control terminal when you log out, and continues to execute without it. To include a list of all the processes without control terminals in a ps output, use the -x option. Here's an example of this effect: $ nohup sleep 10000 & ...nohup a background process. 27406 nohup: appending output to 'nohup.out' ...message from "nohup". $ ps ...look at processes. PID TT STAT TIME COMMAND 27399 pts/3 00:00:00 bash 27406 pts/3 00:00:00 sleep 10000 27407 pts/3 00:00:00 ps $ ^D ...log out. login: glass ...log back in. Password: ...secret. $ ps ...the background process is not seen. PID TT STAT TIME COMMAND 27409 pts/4 00:00:00 bash 27411 pts/4 00:00:00 ps $ ps -x ...the background process may be seen. PID TT STAT TIME COMMAND ... ...along with lots of other output 27406 ? S 00:00 sleep 10000 27409 pts/4 Ss 00:00 bash 27412 pts/4 R+ 00:00 ps -x $ _ For more information about control terminals, consult Chapter 12, "Systems Programming" 5.18.2. Signaling Processes: killIf you wish to terminate a process before it completes, use the kill command. Most shells contain a built-in command called kill in addition to the standard utility. Both versions of kill support the functionality described in Figure 5-18.
In the following example, I created a background process and then killed it. To confirm the termination, I obtained a ps listing: $ (sleep 10; echo done) && ...create background process. 27390 ...process ID number. $ kill 27390 ...kill the process. $ ps ...it's gone! PID TT STAT TIME COMMAND 27355 pts/3 00:00:00 bash 27394 pts/3 00:00:00 ps $ _ The following example illustrates the use of the -l option and a named signal. The signal names are listed in numeric order, starting with signal #1. $ kill -l ...list the signal names. 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 35) SIGRTMIN 36) SIGRTMIN+1 37) SIGRTMIN+2 38) SIGRTMIN+3 39) SIGRTMIN+4 40) SIGRTMIN+5 41) SIGRTMIN+6 42) SIGRTMIN+7 43) SIGRTMIN+8 44) SIGRTMIN+9 45) SIGRTMIN+10 46) SIGRTMIN+11 47) SIGRTMIN+12 48) SIGRTMIN+13 49) SIGRTMIN+14 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX $ (sleep 10; echo done) & 27490 ...process ID number. $ kill -KILL 27490 ...kill the process with signal #9. $ _ 5.18.3. Waiting For Child Processes: waitA shell may wait for one or more of its child processes to terminate by executing the built-in wait command (Figure 5-19).
In the following example, the shell waited until both background child processes had terminated before continuing: $ (sleep 30; echo done 1) & ...create a child process. [1] 24193 $ (sleep 30; echo done 2) & ...create a child process. [2] 24195 $ echo done 3; wait; echo done 4 ...wait for children. done 3 done 1 ...output from first child. done 2 ...output from second child. done 4 $ _ This facility is generally useful only in advanced shell scripts. |