Managing Processes in Ubuntu: kill, pkill, and killall
Managing Processes in Ubuntu: kill, pkill, and killall
In Ubuntu, managing processes is crucial for system administration. Here’s an explanation of how to use kill, pkill, and killall to manage processes, along with examples.
1. kill Command
The kill command is used to send a signal to a process, typically to terminate it. You need to know the Process ID (PID) to use kill.
Syntax:
kill [signal] PID
- Signal: The signal you want to send, e.g.,
-9 for a forceful kill.
- PID: The Process ID of the process you want to terminate.
Example 1: Terminate a process gracefully
$ kill 1234
This sends the default SIGTERM signal to the process with PID 1234, asking it to terminate gracefully.
Example 2: Forcefully terminate a process
$ kill -9 1234
This sends the SIGKILL signal, forcefully terminating the process with PID 1234.
2. pkill Command
pkill allows you to kill processes based on their name, without needing to know the PID.
Syntax:
pkill [options] process_name
Example 3: Terminate all processes with the name firefox
$ pkill firefox
This command will kill all processes with the name firefox.
Example 4: Send a specific signal using pkill
$ pkill -9 firefox
This forcefully kills all processes named firefox.
3. killall Command
killall is similar to pkill but with broader functionality. It can terminate all processes that match a specified name or group.
Syntax:
killall [options] process_name
Example 5: Terminate all instances of chrome
$ killall chrome
This kills all processes with the name chrome.
Example 6: Forcefully terminate all instances of chrome
$ killall -9 chrome
This forcefully kills all chrome processes.
Output Examples
Here’s what you might see after running some of these commands:
Example 7: kill Command Output
$ kill 1234
$ echo $?
0 # A return code of 0 indicates success.
Example 8: pkill Command Output
$ pkill firefox
$ echo $?
0 # If the return code is 0, it means the command was successful.
Example 9: killall Command Output
$ killall chrome
chrome(1234) terminated.
chrome(5678) terminated.
$ echo $?
0 # If the processes are successfully killed, the return code will be 0.
Example 10: Error Handling
$ kill 9999
-bash: kill: (9999) - No such process
$ echo $?
1 # A return code of 1 indicates failure.
Summary
kill: Use when you know the PID.
pkill: Use to kill processes by name, with more control.
killall: Use to kill all processes matching a name.
These commands are powerful tools for managing processes in Ubuntu, helping maintain system stability and control.