The Wayback Machine - https://web.archive.org/web/20231208020835/https://www.tecmint.com/how-to-kill-a-process-in-linux/

How to Kill Linux Process Using Kill, Pkill and Killall

Linux Operating System comes with a kill command to terminate a process. The command makes it possible to continue running the server without the need to 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 96.4% of 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, 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 provide you some extra feature to kill a process by process name using pkill.

Kill Command Usage

The common syntax for kill command is:

# kill [signal or option] PID(s)

For a kill command a Signal Name could be:

Signal Name		Signal Value			Behaviour

SIGHUP			      1				Hangup
SIGKILL			      9				Kill Signal
SIGTERM			      15			Terminate

Clearly from the behavior above, SIGTERM is the default and safest way to kill a process. SIGHUP is a less secure way of killing a process than SIGTERM. SIGKILL is the most unsafe way among the above three, to kill a process that 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 a unique PID is generated for that process.

Every Process in Linux has a pid. The first process that starts when Linux System is booted is the – init process, hence it is assigned a value of ‘1‘ in most cases.

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

Init is the master process and can not be killed this way, which ensures that the master process doesn’t get killed accidentally. Init decides and allows itself to be killed, where kill is merely a request for a shutdown.

List All Running Linux Processes

To know all the processes and correspondingly their assigned pid, run the following ps command.

# ps -A
Sample Output
PID TTY          TIME CMD
    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 Customising the above output using syntax as ‘pidof process‘.

# pidof mysqld
Sample Output
1684

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

# 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/mysqld.pid 
--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/mysqld.pid 
--socket=/var/lib/mysql/mysql.sock

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 Kill a Process in Linux

Before we step ahead and execute a kill command, some important points to be noted:

  • A user can kill all his processes.
  • A user can not kill another user’s process.
  • A user can not kill processes the 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 the ‘pgrep‘ command.

# pgrep mysql
Sample Output
3139

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

kill -9 3139

The above command will kill the process having 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

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

How to Kill a Process in Linux Using Process Name

You must be aware of the process name, before killing and entering a wrong process name may screw you.

# pkill mysqld

Kill more than one process at a time.

# kill PID1 PID2 PID3
or
# kill -9 PID1 PID2 PID3
or
# kill -SIGKILL PID1 PID2 PID3

What if a process has too many instances and a number of child processes, we have a command ‘killall‘ or pkill. These two are the only commands of this family, which takes process name as argument in-place of process number.

Syntax:
# killall [signal or option] Process Name
Or
# pkill Process Name

To kill all mysql instances along with child processes, use the command as follow.

# killall mysqld
OR
# pkill mysqld

You can always verify the status of the process if it is running or not, using any of the below commands.

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

That’s all for now, from my side. I will soon be here again with another Interesting and Informative topic. Till Then, stay tuned, connected to Tecmint, and healthy. Don’t forget to give your valuable feedback in the comment section.

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

40 thoughts on “How to Kill Linux Process Using Kill, Pkill and Killall”

  1. Kill isn’t for kill processes. Kill is for send signals to other processes.
    And no, there is no relationship between uptime and the kill/pkill/etc commands. Most software has a standard start/stop procedure, some of them use kill, but others not.

    Reply
  2. Hey @oke deh, the output of your ‘ps‘ command is showing the ‘grep‘ command, not a process called ‘vnc‘.

    By the time you run the kill command, the grep has completed and so the process id no longer exists, so you can’t kill it.

    Everything you have shown above is working exactly as it is supposed to work, assuming there is no process running on the machine called ‘vnc*‘.

    You should try with just ‘vnc‘, as your grep command is the thing wrong here, not the kill command…

    Reply
  3. Hello tecmint,

    I try to kill this process but its not working…

    "[root@rms ~]# ps -afe|grep vnc*
    root     19784 19688  0 00:02 pts/0    00:00:00 grep vnc*"
    

    I tried following command..

    # kill -9 19784
    

    return this output

    "-bash: kill: (19784) - No such process"
    

    what is the correct command?

    Reply
  4. In order to `kill skypeforlinux` I have used `killall skypeforlinux` two times (because of a wingpanel I believe), but now actually I use this pidof skypeforlinux | xargs kill -9 and it kills my skypeforlinux in one go :) . A good article.

    Reply
  5. I am using kill -9 $pid command to kill my server. However, sometimes if I restart the server, I get an exception as an address already in use that is the port is still in use (port number 6001) even after stopping the server using the above mentioned command.

    Is there any other safe command to use to kill my server so that the port is also freed along with killing of the process

    Reply
    • You can find PID-s of all processes on a port with command *lsof -i :6001*, where 6001 is a number of your port. Then you can kill all processes on that port with *kill* command.

      Reply
  6. Just to say thank you very much Avishek however I would like to know how the root can kill a specific process for an user, please can you give me with a scenario. Thanks again.

    Reply
    • @Martial,
      First find the all the processes and Process IDs of particular users by running following command.

      # ps -aux | grep {user}
      

      And then kill the specific process by their process ID’s

      Reply
  7. Something is messed up. I’ve installed Varnish 3.0 via apt-get install running Raspbian on my Pi 2. For some reason, my “service” commands don’t work. ” sudo service varnish start” yields “varnish: unrecognized service”. Please advise how I can straighten this out. Thanks very much.

    Reply
  8. how to avoid to kill any process. I mean suppose I do not want anybody to kill mysql service using kill command so how can I do this?

    Awaiting your reply,

    Rjas

    Reply
  9. I have problems with chrome slowing down my system, so I’m used to kill all stances of chrome with this command:

    $ while [pidof chrome]; do killall chrome; done

    Reply
    • Dear sriram,
      i Didn’t get you question.
      When you close a terminal. All the running processes gets killed except those running in background and you can kill those by finding their pid.

      Reply
  10. hi…can u tell me the procedure to replace graphic card in red hat linux enterprise 6.0?…my pc was shipped with nvidia 610GT(zotac made GT610) but i need to replace it with nvidia 210GT(asus made En210). the second card is downgraded than the first one.Also can u inform me whether I need to do free OS install or not for this? ur kindness is appreciated on this!!!

    Reply
    • I fear it would be built on the board and perhaps u cant swap it. Ask your Vendor for such detailed hardware information.

      Reply
    • @ Linux Fresher, I didn’t get you properly, although as far as i could understand, you said words for Appraisals for us. Thanks

      Reply
  11. Also xkill is the easiest way to kill a process using just your mouse.

    Just type ‘xkill’ in the terminal and then it would change the cursor to a cross, then just click on the process you want to kill and you are done. It is as easy as that.

    Please add this also. This is very useful in some really frustrating situations like stuck in the middle of a game, then just do Alt+Ctrl+T to bring up the terminal and kill the game using xkill.

    It has helped me quite a lot of times, it might help you also.

    Reply
  12. DO NOT use kill to stop services as a first resort. This will mess up your system if you do it regularly. Use the init scripts already set up for that purpose, or whichever program is provided for control of the service.

    Normally, for example to stop rsyncd, you should use “/etc/init.d/rsync stop”. You can use the same script to start, restart, or get status of the service. Read the script to learn more about what it does.

    If you have Upstart installed, then the “service” command works similarly (but arguably better). The syntax is “service rsync restart” to restart rsyncd, for example. Currently Upstart does not support all services in Debian-based distros, so you might need to use the previous method.

    Many services come with their own maintenance programs separate from the init.d scripts. For example, apache2 httpd comes with apachectl.

    Finally, while the “kill” command is mostly used to stop processes, it can send *any* signal. Check out the manpage for more information. kill is much more useful than explained here- but also more dangerous.

    Reply
    • Killall on Solaris will kill all processes on the system making it unusable.
      so it’s not a good habit to use killall. So use it with extreme care first check then use :-)

      Reply
  13. i usually see interesting topics of linux in this site.i realy like this site ,because it gets me femiliar with linux and i have leant alot upto know.

    thanks

    Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.