Controlling shell processes using signals

Slashdot it! Delicious Share on Facebook Tweet! Digg!
Kirsty Pargeter, 123RF

Kirsty Pargeter, 123RF

Clear Signals

Instead of just killing processes, professionals cleverly catch their communication and steer the computer's tasks in the right direction using signals.

In most cases, kill is used as a last resort. It ends a process without mercy. Systems administrators sometimes kill a process with the kill -9 <PID> command – using the signal 9, which means kill even if the signal is not catchable or ignorable) and the process ID (PID) argument. You can get the PID by using the ps command.

Signals

The standard Linux signals are listed in Table 1. Related systems, such as FreeBSD, have their own variations, with the signals 1, 3, 9, and 15 being the only uniform ones throughout.

Table 1

Signals

1 SIGHUP Separates child from parent process Ends process
2 SIGINT Ends process Same as Ctrl+C
3 SIGQUIT Ends process Process can create core dump
4 SIGILL Ends process After a false call (no privileges, unknown functions)
5 SIGTRAP Ends process Process triggers trace/debugger
6 SIGABRT Ends process From process itself
7 SIGBUS Ends process System call after memory access errors
8 SIGFPE Ends process Division by 0
9 SIGKILL Ends process No data written, risk of data loss
10 SIGUSR1 User-defined signal
11 SIGSEGV Ends process After memory access errors
12 SIGUSR2 User-defined signal
13 SIGPIPE Ends process Pipe error
14 SIGALRM Ends process After timer expiration
15 SIGTERM Ends process Restores files
16 SIGSTKFLT Ends process After co-processor stack error
17 SIGCHLD Ends child process Triggered by parent
18 SIGCONT Continues process
19 SIGSTOP Pauses process
20 SIGTSTP Pauses process Same as Ctrl+Z

You can determine which of the two methods your system uses with kill -l . You can use the kill command in the format kill -<number> <PID> or kill -s <signal_name> <PID> . When using the signal name, you don't need the preceding SIG , so you can either do:

$ kill -KILL 29737

or:

$ kill -9 29737

and it will do exactly the same thing: kill the process instantly.

For some of the examples, the target for the signals will be daemon.sh that I created in Listing 1. The program shows its shell command, its own PID, and the parent process PID in a loop function.

Listing 1

daemon.sh

#! /bin/sh
while true;
do
  echo $0 $$ $PPID
  sleep 1
done

Gently Removed

The basic process-ending method is to use SIGHUP , where the system proceeds as if you had quit the terminal. Conversely, you can prevent this by using nohup at program startup. The application continues to run after you log off, such as with an SSH session.

As long as no one affects the process or shuts down the system, the script runs until it terminates by itself. You can read the output in the nohup.out protocol file by using tail -f nohup.out .

You can restart most system daemons in the same way: kill -HUP <daemon_PID> . It's still recommended, however, to use the daemon's init script or the system's start/stop mechanism instead.

If necessary, you can halt processes to allow other necessary resources to run. For example, the daemon.sh script gets a signal 19 while going through its motions, which pauses the process. You can continue the process using kill -18 <PID> (Figure 1).

Figure 1: Starting, pausing, and continuing a process.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $0.99
(incl. VAT)

Buy Ubuntu User

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content