When we are using a linux operating system , This Operating System comes with Kill command to terminate a process. The command makes it possible to continue running the server without the need of reboot after a major change/update. Here comes the great power of Linux and this is one of the reasons, why Linux is running on 90% of servers, on the planet.
Kill command send a signal, a specified signal to be more perfect to a process. The kill command can be executed in a number of ways, directly or from a shell script.
Using kill command from /usr/bin provide you some extra feature to kill a process by process name using pkill.
The common syntax for kill command is:
#kill [ signal or option ] PID(s)
For using the syntax of kill command we have to know about the PID (Process ID) & Signals.
A list of common Term signals
Linux and Unix-like operating system supports the standard terminate signals listed below:
SIGHUP (1) – Hangup detected on controlling terminal or death of controlling process. Use SIGHUP to reload configuration files and open/close log files.
SIGKILL (9) – Kill signal. Use SIGKILL as a last resort to kill process. This will not save data or cleaning kill the process.
SIGTERM (15) – Termination signal. This is the default and safest way to kill process.
|Signal Name||Signal Value||Behaviour|
Clearly from the behaviour above SIGTERM is the default and safest way to kill a process. SIGHUP is less secure way of killing a process as SIGTERM. SIGKILL is the most unsafe way among the above three, to kill a process which terminates a process without saving.
In order to kill a process, we need to know the Process ID of a process. A Process is an instance of a program. Every-time a program starts, automatically an unique PID is generated for that process. Every Process in Linux, have a pid. The first process that starts when Linux System is booted is – systemd process, hence it is assigned a value of ‘1’ in most of the cases.
systemd is the master process and can not be killed this way, which insures that the master process don’t gets killed accidentally. Init decides and allows itself to be killed, where kill is merely a request for a shutdown.
What is a PID?
A Linux or Unix process is running instance of a program. For example, Firefox is a running process if you are browsing the Internet. Each time you start Firefox browser, the system is automatically assigned a unique process identification number (PID). A PID is automatically assigned to each process when it is created on the system.
For Kill the Process we have to know about the running process. The easiest way to find out what processes are running on your server is to run the top command.
To know all the processes and correspondingly their assigned pid, run.
How about Customising the above output using syntax as ‘pid of process’.
Another way to achieve the above goal is to follow the below syntax.
Before we step ahead and execute a kill command, some important points to be noted:
- A user can kill all his process.
- A user can not kill another user’s process.
- A user can not kill processes System is using.
- A root user can kill System-level-process and the process of any user.
Another way to perform the same function is to execute ‘pgrep’ command.
To kill the above process PID, use the kill command as shown
The above command will kill the process having pid=15, where PID is a Numerical Value of process.
Another way to perform the same function, can be rewritten as.
Similarly ‘kill -9 PID’ is similar to ‘kill -SIGKILL PID’ and vice-versa.
How about killing a process using process name
You must be aware of process name, before killing and entering a wrong process name may screw you.
Kill more than one process at a time.
To Kill more than one process at a time syntax will be
#Kill PID1 PID2 PID3 or #Kill -9 PID1 PID2 PID3 or #Kill -SIGKILL PID1 PID2 PID3
What if a process have too many instances and a number of child processes, we have a command ‘killall’. This is the only command of this family, which takes process name as argument in-place of process number.
#killall [ signal or option ] Process Name
To kill all netns instances along with child processes, use the command as follow
You can always verify the status of the process if it is running or not, using any of the below command.
For knowing about the service is Running or in dead Condition we can use syntax like
#service [name of service] status
After Learning all these Commands & some Illustrations related to kill process you are able to kill a process in Linux operating system like Linux, Ubuntu, RHEL, Mint etc are reading this commands to kill, stop or terminate a process we are also seeing killing a process by using command like pkill or using pid & name of a process with kill command for kill.
Some Terminologies & Commands using above
- Kill – Command use for Kill a process.
- Pkill – Process kill use for kill a process.
- PID – Process ID of a process unique identification of a process.
- Sighup – Signal use to hangup a process.
- Sigkill – Signal use to kill a process.
- Sigterm – Signal use to terminate a process.
- ps -A – Process listing of all processes.
- Pgrep – pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria to stdout.
- Killall – Kill multiple process at a same time.