Section 5.18. Job Control


[Page 188 (continued)]

5.18. Job Control

Convenient 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:

  • ps, which generates a list of processes and their attributes, including their name, process ID number, controlling terminal, and owner

  • kill, which allows you to terminate a process based on its ID number

  • wait, which allows a shell to wait for one of its child processes to terminate

The next few sections describe these facilities in more detail.

5.18.1. Process Status: ps

The ps utility allows you to monitor the status of processes (Figure 5-13).


[Page 189]

Figure 5-13. Description of the ps command.

Utility: ps -efl

ps generates a listing of process status information. By default, the output is limited to processes created by your current shell. The -e option instructs ps to include all running processes. The -f option causes ps to generate a full listing. The -l option generates a long listing. The meaning of each ps column is described in the text that follows.


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.

Figure 5-14. Description of the sleep command.

Utility: sleep seconds

The sleep utility sleeps for the specified number of seconds and then terminates.


The meanings of the common column headings of ps output are given in Figure 5-15.

Figure 5-15. ps output column meanings.

[Page 190]

Column

Meaning

S

The process state.

UID

The effective user ID of the process.

PID

The process ID.

PPID

The parent process ID.

C

The percentage of CPU time that the process used in the last minute.

PRI

The process priority.

SZ

The size of the process's data and stack in kilobytes.

STIME

The time the process was created, or the date if created before today.

TTY

The controlling terminal.

TIME

The amount of CPU time used so far (MM:SS).

CMD

The name of the command.



[Page 190]

The S field encodes the process's state as described in Figure 5-16.

Figure 5-16. Process state codes reported by ps.

Letter

Meaning

O

Running on a processor.

R

Runable.

S

Sleeping.

T

Suspended.

Z

Zombie process.


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 



[Page 191]

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).

Figure 5-17. Description of the nohup command.

Utility: nohup command

The nohup utility executes command and makes it immune to the hangup (HUP) and terminate (TERM) signals. The standard output and error channels of command are automatically redirected to a file called "nohup.out," and the process's priority value is increased by five, thereby reducing its priority. This utility is ideal for ensuring that background processes are not terminated when your login shell is exited.


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.


[Page 192]

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: kill

If 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.

Figure 5-18. Description of the kill command.

Utility/Shell Command: kill [ -signalId ] {pid }+

kill -l

kill sends the signal with code signalId to the list of numbered processes. signalId may be the number or name of a signal. By default, kill sends a TERM signal (number 15), which causes the receiving processes to terminate. To obtain a list of the legal signal names, use the -l option. To send a signal to a process, you must either own it or be a super-user. For more information about signals, refer to Chapter 12, "Systems Programming."

Processes may protect themselves from all signals except the KILL signal (number 9). Therefore, to ensure a kill, send signal number 9 (note that sending a KILL will not allow a process to clean up and terminate normally, as many programs do when they receive a TERM signal).



[Page 193]

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: wait

A shell may wait for one or more of its child processes to terminate by executing the built-in wait command (Figure 5-19).

Figure 5-19. Description of the wait shell command.

Shell Command: wait [ pid ]

wait causes the shell to suspend until the child process with the specified process ID number terminates. If no arguments are supplied, the shell waits for all of its child processes.



[Page 194]

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.




Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net