Blog

03/06/2019

How to end task with PID in Linux



I am a new Linux system admin. How do kill a process from the Linux command line? How can I end task with PID in Linux operating systems?

One an forcibly terminate a Linux process by sending it an appropriate terminate signal. This page shows how to end a task (kill a task) a Linux command line options.

What is a PID in Linux?

A PID is an acronym for the process identification number. PID is automatically assigned to each process when it is created on a Linux operating system. Each process is confirmed with a unique PID. The PID number 1 used by systemd (on older Linux distro it was init). The init or systemd is always the first process on the Linux operating system and is the parent of all other processes.

A list of common Linux TERM signals

Linux operating system support the standard terminate signals listed below:

  1. SIGHUP (1) – Hangup detected on controlling terminal or death of controlling process. Use SIGHUP to reload configuration files and open/close log files.
  2. SIGKILL (9) – Kill signal. Use SIGKILL as a last resort to kill process. It will not save data or cleaning kill the process.
  3. SIGTERM (15) – Termination signal. It is the default and safest way to kill process.

The kill and killall command support more than 60 signals. However, most users only need to use signal 9 or 15. To get a full list of signals, type:
kill -l
Sample outputs:

 1) SIGHUP	 2) SIGINT	 3) SIGQUIT	 4) SIGILL	 5) SIGTRAP
 6) SIGABRT	 7) SIGBUS	 8) SIGFPE	 9) SIGKILL	10) SIGUSR1
11) SIGSEGV	12) SIGUSR2	13) SIGPIPE	14) SIGALRM	15) SIGTERM
16) SIGSTKFLT	17) SIGCHLD	18) SIGCONT	19) SIGSTOP	20) SIGTSTP
21) SIGTTIN	22) SIGTTOU	23) SIGURG	24) SIGXCPU	25) SIGXFSZ
26) SIGVTALRM	27) SIGPROF	28) SIGWINCH	29) SIGIO	30) SIGPWR
31) SIGSYS	34) SIGRTMIN	35) SIGRTMIN+1	36) SIGRTMIN+2	37) SIGRTMIN+3
38) SIGRTMIN+4	39) SIGRTMIN+5	40) SIGRTMIN+6	41) SIGRTMIN+7	42) SIGRTMIN+8
43) SIGRTMIN+9	44) SIGRTMIN+10	45) SIGRTMIN+11	46) SIGRTMIN+12	47) SIGRTMIN+13
48) SIGRTMIN+14	49) SIGRTMIN+15	50) SIGRTMAX-14	51) SIGRTMAX-13	52) SIGRTMAX-12
53) SIGRTMAX-11	54) SIGRTMAX-10	55) SIGRTMAX-9	56) SIGRTMAX-8	57) SIGRTMAX-7
58) SIGRTMAX-6	59) SIGRTMAX-5	60) SIGRTMAX-4	61) SIGRTMAX-3	62) SIGRTMAX-2
63) SIGRTMAX-1	64) SIGRTMAX

How to obtain a PID in Linux

The PID is needed in order to terminate program or taks with the kill command or killall command or pkill command.

How do I locate the process and PID?

To find out PID of firefox/chrome or httpd process use the following pidof command:
pidof firefox
pidof chrome
pidof httpd

Another option is to use the combination of ps command and grep command/egrep command:
ps aux | grep httpd
ps aux | grep firefox
ps aux | grep nginx

Finally, one can use the pgrep command as follows to find the process by name:
pgrep httpd
pgrep firefox
pgrep nginx


The above command indicates that you need to use PID # 22171 to end task with PID in Linux.

How to end task with PID in Linux

The syntax is as follows for the kill command or killall command:

kill -[signal] PID
kill -15 PID
kill -9 PID
kill -SIGTERM PID
kill [options] -SIGTERM PID
killall -15 app_name
killall -9 process_name_such_as_firefox

Let us see how to kill a process or task from the command line using a PID.

Step 1 – Get a PID of firefox process

Run:
pidof firefox
OR
ps aux | grep firefox
OR
pgrep firefox
Sample outputs:

22171

Step 2 – kill a task in Linux using PID

Run:
kill -15 22171
If above failed, run:
kill -9 22171

Step 3 – Verification

Run the following command to make sure a task with PID # 22171 is gone:
ps aux | grep 22171
pidof firefox
pgrep firefox

In this final example, find a pid of backup-job.sh task and kill it:
pgrep backup-job.sh
kill -15 29107

A note about the killall command

This command kill processes by name. No need to find a PID. Say you want to kill taks or process named chrome, just run:
killall -15 chrome
killall -9 nginx

pkill command

The pkill command can look up processes or taks based on name and kill it. The syntax is:
pkill -TERM process
pkill -15 firefox
pkill -9 nginx

Conclusion

This page successfully demonstrated how to terminate task with PID in Linux using various command line options.

Posted by: SXI ADMIN

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

20/08/2019

Start your Bitcoin Exchange with our Software

Start your Bitcoin Exchange with our Software Setup your white label bitcoin exchange right away. Or create your own cryptocurrency using...
14/08/2019

How to KVM, QEMU start or stop virtual machine from command line (CLI)

KVM or Kernel Based Virtual Machine is a popular virtualization technology. It allows you to run virtual guest machines over a host machine. To start...
14/08/2019

How to Docker backup Saving and restoring your volumes

Running a Docker volume backup First, we spin up a temporary container, and we mount the backup folder and the target Docker volume to this container....