Killing a process safely

Video 124 of 140 in Virtualization Foundations, in the unit “Processes, signals, /proc and /sys”. 3:27, with English and Hindi audio.

Sometimes a program must be stopped now. The quickest way is also the most risky, because a hard stop can leave files half written and cause a second problem. This article shows a safer order: confirm the right process ID, ask the program to stop with TERM, wait and check, and only then use KILL. It also lists the mistakes that cost the most, such as ending the wrong copy of a program or stopping a virtual machine's process by habit.

What you will learn

A runaway job at the end of a shift

It is the end of a shift on a warehouse server. A report job is looping, eating CPU, and the team is waiting for the server to recover. The quickest fix is to end the job at once. But a hard stop can leave a half-written file, and that causes a new problem. The safe way takes a few more steps.

Find the process and confirm it

Start by finding the process. The pgrep command with the a option lists the processes whose names match, with their process IDs and full command lines.

List processes whose names match report, with their IDs
pgrep -a report

Before you act, confirm the process ID with ps. Check the owner, the start time, and the command, so that you know this is the process you mean.

Confirm the owner, start time and command of process 4312
ps -p 4312 -o pid,user,lstart,cmd

Ask politely, then check

Send TERM with kill, which asks the program to save its work and leave. Then wait a few seconds, and check again. The sleep part of the next line waits ten seconds before ps looks at the process.

Ask process 4312 to stop, with TERM
kill 4312
Wait ten seconds, then check whether the process is still there
sleep 10; ps -p 4312

Note: If the process has gone, ps prints only its header line. If it is still there, ps prints its row.

Force only as a last resort

KILL is the last step, and it is used only after TERM has failed. KILL cannot be caught, so the program gets no chance to clean up. A half-written file can open without an error, and still be incomplete, so check the data afterwards.

Force the end of process 4312, after TERM failed
sudo kill -KILL 4312

Mistakes that cost the most

After a forced kill, a program may refuse to start again, because a lock file it left behind still says it is running. If a service manager restarts the program, the job may come right back. Stopping the process is not the same as stopping the service.

Try it yourself

Use only a process that you own and may stop, and replace 4312 with the process ID that your own list shows. Work through the steps in order, and stop before KILL if TERM has worked.

  1. List the processes whose names match report, with their process IDs.

    pgrep -a report

    What you should see: The list shows the matching processes, with their IDs and full command lines.

  2. Confirm the owner, start time and command of the process. Use your own process ID in place of 4312.

    ps -p 4312 -o pid,user,lstart,cmd

    What you should see: The row shows who owns the process, when it started, and its command.

  3. Ask the process to stop, with the polite TERM signal that kill sends by default.

    kill 4312
  4. Wait ten seconds, then check whether the process is still there.

    sleep 10; ps -p 4312

    What you should see: If the process has gone, only the header line is printed. If it is still there, its row is printed.

  5. Only if the process is still there after TERM, force it to end. KILL gives no chance to clean up.

    sudo kill -KILL 4312

Terms

TERM
The polite stop signal that asks a program to save its work and exit, and that kill sends by default.
KILL
The forced stop signal that ends a process at once, with no chance for the program to clean up.

Check yourself

Why confirm the process ID before sending a signal?

Two copies of one program can share a name. The process ID is the only way to be sure the signal reaches the right one.

When is KILL the right choice?

Only after TERM has failed and the process is still there. KILL gives no chance to save work or clean up.

Why is stopping a virtual machine's process like pulling the power?

On a virtualization host, each virtual machine runs as one process, so ending that process stops the machine inside it at once.

Previous: What each signal means: TERM, KILL, HUP, STOP and CONT. Next: Foreground, background, and surviving a logout with nohup.