Updated April 20, 2023
Introduction of Linux Terminate Process
In the Linux ecosystem, when we are running any application or any job. Every application or job will run with a specific process ID or daemon or service. The process ID is nothing but a digit number. Sometimes the job or any service may not run due to some issues like deadlock, process stuck, etc. Due to which remaining jobs will impact coming from the same service. If such a condition may happen, we need to kill or terminate the stuck job or process.
Syntax of Terminate Process
ps [ OPTION ] kill [ OPTION ] [ PROCESS ID ]
- ps: We can use the “ps” keyword in the syntax. It will accept the arguments as an option. As per the provided input arguments, the “ps” command will display the information of all the running processes in the environment.
- kill: We can use the “kill” keyword in the syntax or command to terminate the process. It will accept the two arguments like OPTION and PROCESS ID. As per the provided input arguments, the “kill” command will terminate the process.
- OPTION: We can provide the different flags as options that are compatible with the “ps” and “kill” command.
- PROCESS ID: We need to provide the relevant process ID to terminate the process.
How Linux Terminate Process Works?
As we have discussed earlier, every job or application is running with a specific process ID. In the background, we are able to get all the list of the running process in the Linux environment. Due to some technical issues like deadlock, process or services stuck the job or application will not able to run. Hence we need to terminate the stuck process. So the next upcoming jobs will be executed perfectly fine.
In the Linux environment, if we need to kill the process. First, we need to identify the process and get the relevant process id. There are different ways to get the process id from the Linux environment like “ps” and “top”.
"ps" command. ps -ax
To terminate the process, we need to use the below different killing signals in the kill command.
- To use SIGHUP signal, we need to use the digit “1”
- It is known as the hang up signal
- To use SIGINT signal, we need to use the digit “2”
- It is known as the interrupt signal
- To use SIGINT signal, we need to use the digit “17, 19, 23”
- It is known as the stop signal
- To use SIGTERM signal, we need to use the digit “15”
- It is known as the termination signal
- To use SIGTERM signal, we need to use the digit “9”
- It is known as the kill signal
Examples to Implement Linux Terminate Process?
Following are the examples are given below:
1. Identify the Linux Process
While terminating the Linux process, it is very important to identify the running process. There are different ways to identify the running process.
ps -aux ps -aux | grep -i "httpd"
Explanation: As per the above command, we are able to get all the running process information on the Linux server. From the first “ps” command, we are the entire list down all the process (refer to screenshot 1 (a)). But it is very difficult to identify the specific process. To overcome this condition, we need to use the “pipe (|)” functionality and grep the data from it (refer to screenshot 1 (b)).
2. Terminate Process– With Kill Command
In termination of the Linux process, we can use the kill command. The kill command sends an exact signal to the process. In different ways, we can trigger the command like a direct command or via shell.
kill -9 1941
Explanation: As discussed earlier, we are having different signals in the kill command. As per the requirement, we can use those signals. In the above “kill” command. We are using the kill signal i.e. “9”. The process id “1941” is running in the environment. We are terminating the “1941” process with the help of kill signal (9).
3. Terminate Process– With pkill Command
In the Linux environment, we are having the functionality to kill the process with the help of the process name.
Note: There is no need for a process id here.
Explanation: Sometimes we don’t have time to check all process ids and terminate the process accordingly. Hence we need functionality to kill the process via service, process or application name. To terminate the process, we can use the process name with pkill command. As per the below screenshot, we have used the “https” as the process name. It will terminate all the processes running with the same name.
4. Terminate Process– With kill all Command
We can terminate all the processes as well as the running child processes. Here, we just need to specify the process name only.
Explanation: If we need to terminate all the processes like a parent as well as child process both then we need to use the “killall” command. As per the below screenshot, we are using the “httpd” process name in the “killall” command. It will terminate all the parent and child processes. Once, the “killall” command will terminate the process. The status of the service or application is in a failed state.
We have seen the uncut concept of “Linux Terminate Process” with the proper example, explanation, and command with different outputs. The process termination is a very important activity in terms of server management. With the help of the termination process, we can kill the deadlock process or stuck process. Due to which the next upcoming jobs will not affect. Hence we are able to keep continuous server or application productivity.
We hope that this EDUCBA information on “Linux Terminate Process” was beneficial to you. You can view EDUCBA’s recommended articles for more information.