1. Introduction
As system administrators, we may encounter a situation where we need to kill a process that’s running on our system. Several reasons necessitate doing this, such as when a process becomes stuck, excessively uses system resources, or fails to respond to commands.
In this tutorial, we’ll discuss how to kill a process based on its process arguments.
2. Why Kill a Process Based on the Process Arguments?
Process arguments are the parameters that are passed to a process when it’s started. These arguments help the process to determine its behaviour and determine the actions it should perform.
When there are multiple instances of the same process running on a system, killing the process based on its arguments can be more precise than killing the process based on its PID (Process ID).
There are several tools that we can use to kill a process based on the process arguments. The most commonly used tools are the command-line tools such as ps, kill, and pkill.
3. Terminating a Process Based on Process Arguments
To terminate a process based on the process arguments, we can use a combination of the ps and kill commands. The ps command provides information about the status of processes running on a system, while the kill command terminates a process based on its PID.
Here’s an example of using the ps and kill commands to terminate a process based on the process argument:
$ ps -eo pid,args --cols=10000 | awk '/process_name/ && $1 != PROCINFO["pid"] { print $1 }'
Output: 2696
$ kill `ps -eo pid,args --cols=10000 | awk '/process_name/ && $1 != PROCINFO["pid"] { print $1 }'`
In this example, the first command ps -eo pid,args –cols=10000 | awk ‘/process_name/ && $1 != PROCINFO[“pid”] { print $1 }’retrieves the PID of the process with the argument process_name. The ps command is used with the following options and arguments:
- -e: This option specifies that all processes running on the system should be displayed.
- -o pid,args: This option specifies the columns to be displayed, in this case PID and arguments of the processes.
- –cols=10000: This option sets the width of the output to a large value to prevent wrapping of output lines.
- awk: This is a programming language used to filter and manipulate data. In this case, it’s used to match the processes with the argument process_name and to print only the PID. The awk command has the following arguments:
- /process_name/: This argument matches the processes with the argument process_name.
- && $1 != PROCINFO[“pid”] { print $1 : This argument is used to filter out the PID of the current shell process so that it’s not killed.
The output of this command is 2696, which is the PID of the process we want to terminate.
The second command kill terminates the process with PID 2696. The PID is obtained from the output of the previous command, which is passed to the kill command using backticks (“). We can use the kill command to terminate a process by providing its PID as the single argument.
4. Killing a Process With the pkill Command
The pkill command is similar to kill, but it allows us to kill a process based on its name or process arguments.
Let’s look at the format of the pkill command:
$ pkill -f process_arguments
Here process_arguments is the argument we want to use to kill the process.
For example, let’s kill a process that’s running a script called script.sh:
$ pkill -f script.sh
The pkill command then kills all the instances of the process that are running the script.sh script.
5. Killing a Process With the killall Command
We can use the killall and pkill commands to terminate processes in a Unix-like operating system. However, the main difference between the two is in their scope of operation. The killall command terminates all processes with the same name, whereas the pkill command targets all instances of a process that match specified criteria, such as the script name or user name running the process. In other words, killall operates based on process names, while pkill operates based on more specific attributes of a process.
Let’s look at the the format for killall command:
$ killall process_name
Here process_name is the name of the process we want to kill.
For example, if we want to kill all processes that are running the Apache web server, we’d use the following command:
$ killall apache2
6. Conclusion
In this article, we discussed how to kill a process based on its process arguments. There are several tools we can use to do this, including the ps, kill, pkill, and killall commands.
It’s important to note that killing a process should be a last resort and should only be done when all other options have been exhausted. Before killing a process, it’s a good idea to try and find out what’s causing the problem and try to resolve it in a more gentle manner.
In conclusion, killing a process based on its process arguments can be a useful technique for system administrators to have in their toolkit. By understanding the different tools available, we can quickly and effectively kill processes that are causing problems in our system.