How to end the Linux process with Kill, Pkill, and Killall

s2 0

Linux operating system comes with a Kill order to end an operation. The command allows the server to continue operating without having to restart after a major change / update. This is where the great power of Linux comes in and this is one of the reasons Linux runs 96.4% from servers on the planet.

Kill Command sends a signal, a specified signal, to a currently running process. The kill command can be executed in a number of ways, either directly or from a shell script.

[ You might also like: Find Top 15 Processes by Memory Usage with ‘top’ in Batch Mode ]

Using kill Command from / usr / bin Offer some additional functions for terminating a process based on the process name pkill.

Use of kill orders

The common syntax for Kill order is:

# kill [signal or option] PID(s)

For a Kill order a Signal name could be:

Signal Name		Signal Value			Behaviour

SIGHUP			      1				Hangup
SIGKILL			      9				Kill Signal
SIGTERM			      15			Terminate

From the above behavior it is clear that SIGTERM is the standard and safest way to end a process. SIGHING is a less secure way of killing a process than SIGTERM. SIGKILL is the least secure way of killing a process that kills a process without saving.

To kill a process, we need to know the process ID of a process. A procedure is an instance of a program. Each time the program starts, a unique one is automatically created PID is generated for this process.

Every process in Linux has a pid. The first process started when booting the Linux system is the – init processso it is assigned the value ‘1‘ in most cases.

[ You might also like: All You Need To Know About Processes in Linux [Comprehensive Guide] ]

Inside is the master process and cannot be terminated this way, which ensures that the master process does not accidentally terminate. Inside decides and lets himself be killed, whereby kill is only a request to shut down.

List all running Linux processes

Know all processes and their assignments pid, run the following ps command.

# ps -A
Sample output
    1 ?        00:00:01 init
    2 ?        00:00:00 kthreadd
    3 ?        00:00:00 migration/0
    4 ?        00:00:00 ksoftirqd/0
    5 ?        00:00:00 migration/0
    6 ?        00:00:00 watchdog/0
    7 ?        00:00:01 events/0
    8 ?        00:00:00 cgroup
    9 ?        00:00:00 khelper
   10 ?        00:00:00 netns
   11 ?        00:00:00 async/mgr
   12 ?        00:00:00 pm
   13 ?        00:00:00 sync_supers
   14 ?        00:00:00 bdi-default
   15 ?        00:00:00 kintegrityd/0
   16 ?        00:00:00 kblockd/0
   17 ?        00:00:00 kacpid
   18 ?        00:00:00 kacpi_notify
   19 ?        00:00:00 kacpi_hotplug
   20 ?        00:00:00 ata/0
   21 ?        00:00:00 ata_aux
   22 ?        00:00:00 ksuspend_usbd

How about adapting the above output with the syntax as’pidof process‘.

# pidof mysqld
Sample output

Another way to achieve the above goal is to follow the syntax below.

# ps aux | grep mysqld
Sample output
root      1582  0.0  0.0   5116  1408 ?        S    09:49   0:00 
/bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql 
--socket=/var/lib/mysql/mysql.sock --pid-file=/var/run/mysqld/ 
--basedir=/usr --user=mysql

mysql     1684  0.1  0.5 136884 21844 ?        Sl   09:49   1:09 
/usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql 
--log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/ 

root     20844  0.0  0.0   4356   740 pts/0    S+   21:39   
0:00 grep mysqld

[ You might also like: How to Find Top 15 Processes by Memory Usage in Linux ]

How to end a process in Linux

Before we go a step further and a. carry out Kill orderto note some important points:

  • A user can terminate all of his processes.
  • One user cannot end another user’s process.
  • A user cannot terminate processes that the system is using.
  • A root user can kill the system level process and any user’s process.

Another way to perform the same function is to use the ‘pgrep‘Command.

# pgrep mysql
Sample output

To finish the above process PID, use the kill command as shown.

kill -9 3139

The above command terminates the process with pid=3139, where PID is a Numerical value of the process.

Another way to perform the same function can be rewritten as.

# kill -SIGTERM 3139

Similar ‘kill -9 PID‘ is similar to ‘kill -SIGKILL PID‘ and vice versa.

How to end a process in Linux with the process name

You must be aware of the process name before you can exit the process and enter an incorrect process name.

# pkill mysqld

End more than one process at the same time.

# kill PID1 PID2 PID3
# kill -9 PID1 PID2 PID3

What if a process has too many instances and a number of child processes, we have a command ‘kill all‘ or pkill. These two are the only commands in this family that take the process name as an argument instead of the process number.

# killall [signal or option] Process Name
# pkill Process Name

To kill everyone mysql instances Use the command as follows in conjunction with child processes.

# killall mysqld
# pkill mysqld

You can always check the status of the process, whether it is running or not, by using one of the following commands.

# service mysql status
# systemctl status mysql
# pgrep mysql
# ps -aux | grep mysql

That’s it from my side for now. I’ll be back here soon with another interesting and informative topic. Until then, stay tuned, connected to Tecmint, and healthy. Don’t forget to give yours valuable feedback in the comments section.