-->
Previous Table of Contents Next


The entry in the second column of the ps listing is the PID of the process. In the following example, three processes are running for the current user (in addition to the shell). The current user’s name is pcoco.


$ ps -ef | grep $LOGNAME

 pcoco 11805 11804  0  Dec 22   ttysb    0:01 sort sales.dat>sales.srt

 pcoco 19955 19938  4  16:13:02 ttyp0    0:00 grep pcoco

 pcoco 19938     1  0  16:11:04 ttyp0    0:00 bash

 pcoco 19940 19938 142 16:11:04 ttyp0    0:33 find. -name core -exec rm {};

$

To lower the priority on the process with PID 19940 (the find process), enter the following:


renice -5 19940

As you would expect, the following statements are true about renice:

  You can use renice only with processes you own.
  The superuser can use renice on any process.
  Only the superuser can increase the priority of a process.

Terminating Processes with kill

Sometimes, you want or need to terminate a process. The following are some reasons for stopping a process:

  It’s using too much CPU time.
  It’s running too long without producing the expected output.
  It’s producing too much output to the screen or to a disk file.
  It appears to have locked a terminal or some other session.
  It’s using the wrong files for input or output because of an operator or programming error.
  It’s no longer useful.

Most likely, you’ll come across a number of other reasons to kill a process as well. If the process to be stopped is a background process, use the kill command to get out of these situations.

To stop a command that isn’t in the background, press <Ctrl-c>. When a command is in the background, however, pressing an interrupt key doesn’t stop it. Because a background process isn’t under terminal control, keyboard input of any interrupt key is ignored. The only way you can stop background commands is to use the kill command.

Normal Termination of Background Processes

The kill command sends signals to the program to demand that a process be terminated or killed. To use kill, use either of these forms:


kill PID(s)

or


kill -signal PID(s)

To kill a process whose PID is 123, enter kill 123. To kill several processes whose PIDs are 123, 342, and 73, enter kill 123 342 73.

By using the -signal option, you can do more than simply kill a process. Other signals can cause a running process to reread configuration files or stop a process without killing it. Valid signals are listed by the command kill -l. An average user, however, will probably use kill with no signal or, at most, with the -9 signal (the I-mean-it-so-don’t-ignore-me signal, described in the next section).


CAUTION:  
Use the correct PID with the kill command. Using the wrong PID can stop a process you want to keep running. Remember that killing the wrong process or a system process can have disastrous effects. Also remember that if you’re logged in as the system administrator, you can kill any process.

If you successfully kill the process, you get no notice from the shell; the shell prompt simply reappears. You see an error message if you try to kill a process you don’t have permission to kill or if you try to kill a process that doesn’t exist.

Suppose that your login name is chris and that you’re now logged in to tty01. To see the processes you have running, enter ps -f, and you’ll see the following response:


UID        PID   PPID  C   STIME      TTY     TIME   COMMAND

chris      65     1    0   11:40:11   tty01   0:06   -bash

chris      71    65   61   11:42:01   tty01   0:14   total_updt

chris     231    65   80   11:46:02   tty01   0:00   ps -f

chris     187    53   60   15:32:01   tty02 123:45   crunch stats

chris      53     1    0   15:31:34   tty02   1:06   -bash

Notice that the program total_updt is running at your current terminal. Another program, crunch, is running on another terminal, and you think it has used an unusually large amount of CPU time. To kill that process, it may be sufficient to enter kill 187. To kill the parent of that process, enter kill 53.

You may want to kill a parent and its child if you logged in as the system administrator and see that someone left their terminal unattended (if you’ve set up Linux with remote terminals). You can kill a clock process that the user has running (the child process) and the login shell (the parent process) so that the unattended terminal is no longer logged in.

Stopping the parent of a process sometimes terminates the child process as well. To be sure, stop the parent and its children to halt all activity associated with a parent process. In the preceding example, enter kill 187 53 to terminate both processes.


TIP:  If your terminal locks up, log in to another virtual terminal by pressing <Alt> combined with a function key (F1–F6), enter ps -ef | grep $LOGNAME, and then kill the login shell for the locked terminal.

Unconditional Termination of Background Processes

Issuing the kill command sends a signal to a process. Linux programs can send or receive more than 20 signals, each of which is represented by a number. For example, when you log out, Linux sends the hang-up signal (signal number 1) to all the background processes started from your login shell. This signal kills or stops those processes unless they were started with nohup (as described earlier in this chapter).

Using nohup to start a background process lets the process ignore the signal that tries to stop it. You may be using programs or shell scripts written to ignore some signals. If you don’t specify a signal when you use kill, signal 15 is sent to the process. The command kill 1234 sends signal 15 to the process whose PID is 1234. If that process is set to ignore signal 15, however, the process doesn’t terminate when you use this command. You can use kill in a way that a process “can’t refuse,” however.

The signal 9 is an unconditional kill signal; it always kills a process. To unconditionally kill a process, use the following command:


kill -9  PID


Previous Table of Contents Next