05. Pausing a process kill, killall

To pause or stop a process, we can either use keyboard shortcuts or send a signal to our process. Let's look at the keyboard shortcuts first, then dive into what sending signals is all about.

Interrupting a Process

To interrupt a process, press ctrl+c. This interrupts the program by sending it a INT (interrupt) signal.

Pausing a Process

To pause a process, press ctrl+z. This sends the program a TSTP command (Terminal Stop).

This is especially useful if you want to pause a program and come back to it later. For example, we could have Python running, then press ctrl+z. This will place Python to the background. If you want to return to Python, simply type fg %1, and you'll be back where you left off.

Stopping a process

Keyboard shortcuts are simply a convenient way to sending signals to jobs in our foreground.

As we have just seen, ctrl+c sends an INT signal (for interrupt), and ctrl+z sends a TSTP signal (terminal stop).

Besides sending an INT (interrupt) or TSTP (terminal stop) signal, there are many more signals that we can use with the kill command.

To use this command, specify a signal as the option, with the PID or the jobspec.

$ kill -s signal pid

If no signal is mentioned, the default signal is TERM (terminate).

Here is a list of Unix signals, which you may also see with the kill -l command. When specifying the signal, you may either use the shortened word form, or its integer value. We've included an asterisk next to a signal to denote the most commonly used signals.

1 HUP*
SIGHUP. Hang up.
2 INT
Interrupt (same as ctrl+c). Terminates a program.
3 QUIT
Quit.
9 KILL*
Kill. This signal is not sent to the target program. Instead, the kernel immediately stops the process, which doesn't allow for a proper clean up.
11 SEGV
Segmentation violation. Sent if program makes illegal use of memory, such as writing data where it's not permitted.
15 TERM*
Terminate. The default signal sent by the KILL command.
18 CONT
Continue. This restores a process after a STOP signal.
19 STOP
Causes a process to pause without terminating. It is not send to the target process, like KILL.
20 TSTP
Terminal stop. (same as ctrl+z).
28 WINCH
Window change. A signal sent when the window changes size - some programs such as top and less respond by readjusting itself to fit into the new window.

Here are a few examples with the above signals.

$ kill -3 13569
# Quits out of process with PID 13569
$ kill -INT 1239
# Interrupts process with PID 1239.

Running processes persistently

Even without the kill command, a process can be shutdown. For instance, if you ssh into a computer, start up a process, then logout, a SIGHUP command will be sent out, and your program will be shut down.

To prevent your process to shutting down, precede the process invokation with the nohup command.

$ nohup python3 MyRedditBot.py

Shutting down multiple processes

To shut down multiple processes, use the killall command. This command uses a process' name instead of its PID. You may also place in the -i option so that the terminal asks for confirmation before shutting down a process.

The following command will terminal all programs with the name gedit.

$ killall gedit

Take your Linux skills to the next level!

How Linux Works

Take your Linux skills to the next level! Try Linux & UNIX

In this completely revised second edition of the perennial best seller How Linux Works, author Brian Ward makes the concepts behind Linux internals accessible to anyone curious about the inner workings of the operating system. Inside, you'll find the kind of knowledge that normally comes from years of experience doing things the hard way.

$ Check price
39.9539.95Amazon 5 logo(114+ reviews)

More Linux & UNIX resources

Aching back from coding all day?

Foam Seat Cushion

Aching back from coding all day? Try Back Problems

This foam seat cushion relieves lowerback pain, numbness and pressure sores by promoting healthy weight distribution, posture and spine alignment. Furthermore, it reduces pressure on the tailbone and hip bones while sitting. Perfect for sitting on the computer desk for long periods of time.

$ Check price
99.9599.95Amazon 4.5 logo(9,445+ reviews)

More Back Problems resources

Ad