Clear Signals
Instead of just killing processes, professionals cleverly catch their communication and steer the computer's tasks in the right direction using 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.
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
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).
Pages: 4
With the snazzy little program GNU Parallel, you can make use of the full power of your multicore CPUs through scripts.
Keeping an eye on possible changes to the filesystem can prove very beneficial. It can also help you to automate many processes.
With a small script, you can convert large amounts of scanned text into PDF files that you can then browse with typical Linux tools – all thanks to OCR.
Both init and systemd can be used to start, monitor, and shut down services on Linux systems, but these utilities differ in terms of configuration and operation.
From simple queries to complex menus: Using dialog, you can create a graphical interface for shell scripts with only a few extra lines of code.
© 2024 Linux New Media USA, LLC – Legal Notice