Introduction
In this lab, you will learn how to use the Linux pkill command to terminate processes by their name or process ID (PID). The pkill command is a powerful tool for managing processes in a Linux system, as it provides a convenient way to kill processes without having to know their exact PIDs. You will explore how to terminate processes by name, as well as how to terminate processes by their PID. The lab covers practical examples and demonstrates the versatility of the pkill command in process management tasks.
Understand the pkill Command
In this step, you will learn about the pkill command in Linux, which allows you to terminate processes by name or process ID (PID).
The pkill command is a powerful tool for managing processes in a Linux system. It provides a convenient way to kill processes without having to know their exact PIDs. This can be especially useful when you need to terminate multiple instances of a process or when you're not sure of the exact PID of the process you want to terminate.
To use the pkill command, you can specify the process name or a pattern that matches the process name. The command will then terminate all matching processes.
Here's an example of how to use pkill to terminate a process by name:
sudo pkill -f firefox
This command will terminate all processes with the name "firefox".
Example output:
[1]+ Terminated firefox
You can also use the -9 option to send a SIGKILL signal to the process, which will force it to terminate immediately:
sudo pkill -9 -f firefox
This command will terminate all processes with the name "firefox" using the SIGKILL signal.
Example output:
[1]+ Killed firefox
In addition to terminating processes by name, you can also use pkill to terminate processes by their PID. To do this, you can use the -P option followed by the PID:
sudo pkill -P 12345
This command will terminate the process with the PID of 12345.
Example output:
[1]+ Terminated process 12345
Remember, the pkill command is a powerful tool, so use it with caution to avoid accidentally terminating important system processes.
Terminate Processes by Process Name
In this step, you will learn how to use the pkill command to terminate processes by their process name.
First, let's start a few processes that we can use for this example:
## Start a few processes
sleep 1000 &
firefox &
gedit &
Now, let's use pkill to terminate the Firefox process by its name:
sudo pkill -f firefox
Example output:
[2]+ Terminated firefox
As you can see, the Firefox process has been terminated.
You can also use wildcards with the pkill command to match multiple processes by name. For example, to terminate all processes that have "gedit" in their name:
sudo pkill -f gedit
Example output:
[3]+ Terminated gedit
If you want to terminate all processes that match a specific pattern, you can use the -e option to specify a regular expression:
sudo pkill -ef 'sleep|firefox'
This command will terminate all processes that have "sleep" or "firefox" in their name.
Example output:
[1]+ Terminated sleep 1000
[2]+ Terminated firefox
Remember, the pkill command is a powerful tool, so use it with caution to avoid accidentally terminating important system processes.
Terminate Processes by Process ID
In this step, you will learn how to use the pkill command to terminate processes by their process ID (PID).
First, let's start a new process that we can use for this example:
## Start a new process
sleep 1000 &
To get the PID of the sleep process, you can use the ps command:
ps -ef | grep sleep
Example output:
labex 3456 3455 0 11:30 pts/0 00:00:00 sleep 1000
In this example, the PID of the sleep process is 3456.
Now, let's use pkill to terminate the process by its PID:
sudo pkill -P 3456
Example output:
[1]+ Terminated sleep 1000
As you can see, the sleep process has been terminated.
You can also use the -9 option to send a SIGKILL signal to the process, which will force it to terminate immediately:
sudo pkill -9 -P 3456
This command will terminate the process with the PID of 3456 using the SIGKILL signal.
Example output:
[1]+ Killed sleep 1000
Remember, the pkill command is a powerful tool, so use it with caution to avoid accidentally terminating important system processes.
Summary
In this lab, you learned about the pkill command in Linux, which allows you to terminate processes by name or process ID (PID). You explored how to use pkill to terminate processes by their name, providing examples of terminating all processes with the name "firefox" using the default SIGTERM signal or the SIGKILL signal to force immediate termination. Additionally, you learned how to terminate processes by their PID using the -P option. The pkill command is a powerful tool for managing processes in a Linux system, but it should be used with caution to avoid accidentally terminating important system processes.



