How to Safely Kill Linux Processes: kill, killall, and pkill Explained
This guide explains how to terminate Linux processes using the kill, killall, and pkill commands, covering signal types, how to find process IDs, command syntax, and practical examples for both foreground and background tasks.
Understanding Linux termination signals
When a process is stopped by the operating system or a user, a termination signal is sent. The most common signals are:
SIGHUP (1) : Hangup – sent when the controlling terminal closes.
SIGINT (2) : Interrupt – generated by Ctrl+C in the terminal.
SIGKILL (9) : Kill – forces immediate termination, cannot be caught.
SIGTERM (15) : Termination – default signal for kill, allows graceful shutdown and can be handled by the process.
SIGSTOP (19/17/23/24) : Stop – pauses a process; it can be resumed later.
Using kill to terminate a process
The kill command requires the target process ID (PID) and optionally a signal. If no signal is specified, SIGTERM (15) is used. kill [signal] <PID> Example: start a background sleep job and then terminate it.
$ sleep 120 &
[1] 125686
$ kill 125686
[1]+ terminated sleep 120You can also specify the signal by name or number:
$ kill -SIGKILL 125746
$ kill -9 125759Finding a process ID
Before using kill, you need the PID. The pidof command returns the PID(s) of a given executable name. pidof exact_process_name Example for Java processes:
pidof java
8075 1032Using killall to terminate by name
If you do not know the PID or want to kill all instances of a program (including child processes), use killall. The syntax is similar to kill and defaults to SIGTERM (15) when no signal is given. killall [signal] <process-name> Example: kill all running sleep commands.
$ sleep 120 &
[1] 112351
$ sleep 2000 &
[2] 112362
$ killall sleep
[1]- Terminated sleep 120
[2]+ Terminated sleep 2000Using pkill for pattern‑based termination
The pkill command combines pgrep pattern matching with kill. It can match processes by name, owner, or other criteria. pkill [options] pattern Useful options include: -u: match processes owned by a specific user. -x: require an exact name match. -signal: specify the termination signal (default SIGTERM).
Example: terminate all nginx processes.
pkill nginxSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
