How to PROPERLY kill process on Ubuntu? [SOLVED]


Written By - Omer Cakmak
Advertisement

In operating systems (Windows, Linux does not matter), things are managed as processes. These processes run for a certain time, some processes are terminated by admins and some are terminated by different applications. Terminating a process means stopping it.

In this article, let's explain how to terminate processes on Ubuntu, a widely used Debian based distribution.

 

How to list process?

To terminate a process, the process ID of that process must be known. The article "5 practical examples to list running processes in Linux" will help with this. Let's show some methods in this article.

 

Method-1: Use ps command

List the processes running with the commonly used ps -aux command and parameter:

foc@ubuntu22:~$ ps -aux
USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.3 102188 12980 ?        Ss   09:02   0:08 /lib/systemd/systemd --system --deserialize 35
root         430  0.0  0.0      0     0 ?        I<   09:02   0:00 [kmpath_handlerd]
root         539  0.0  0.0      0     0 ?        S    09:02   0:00 [jbd2/vda2-8]
root         541  0.0  0.0      0     0 ?        I<   09:02   0:00 [ext4-rsv-conver]
message+     655  0.0  0.1   9100  5036 ?        Ss   09:02   0:01 @dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activat
root         662  0.0  0.4  32648 17540 ?        Ss   09:02   0:00 /usr/bin/python3 /usr/bin/networkd-dispatcher --run-startup-triggers
root         671  0.0  0.1  23668  7456 ?        Ss   09:02   0:00 /lib/systemd/systemd-logind
root         706  0.0  0.0   6172  1152 tty1     Ss+  09:02   0:00 /sbin/agetty -o -p -- \u --noclear tty1 linux
root         719  0.0  0.4 109712 20040 ?        Ssl  09:02   0:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --w
root        1046  0.0  0.2  17164 10508 ?        Ss   10:13   0:00 sshd: foc [priv]
foc         1049  0.0  0.2  17016  9820 ?        Ss   10:13   0:00 /lib/systemd/systemd --user
foc         1050  0.0  0.0 103740  3620 ?        S    10:13   0:00 (sd-pam)
foc         1101  0.0  0.1  17300  7540 ?        S    10:13   0:00 sshd: foc@pts/0
foc         1102  0.0  0.1   8776  5016 pts/0    Ss   10:13   0:00 -bash
systemd+   93655  0.0  0.1  89352  6584 ?        Ssl  18:05   0:00 /lib/systemd/systemd-timesyncd
root       94295  0.0  0.0      0     0 ?        I    18:05   0:00 [kworker/0:4-events]
syslog     94756  0.0  0.1 222400  4040 ?        Ssl  18:05   0:00 /usr/sbin/rsyslogd -n -iNONE
root      116474  0.0  0.0      0     0 ?        I    18:05   0:00 [kworker/u4:4-events_power_efficient]
root      117462  0.2  0.9 874820 36820 ?        Ssl  18:05   0:01 /usr/lib/snapd/snapd
root      117726  0.0  0.0   6892  1300 ?        Ss   18:08   0:00 /usr/sbin/cron -f -P
root      117727  0.0  0.0  82796  3880 ?        Ssl  18:08   0:00 /usr/sbin/irqbalance --foreground
root      117732  0.0  0.5 295544 20840 ?        Ssl  18:08   0:00 /usr/libexec/packagekitd
root      117733  0.0  0.2  15420  8960 ?        Ss   18:08   0:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
root      117737  0.0  0.1 234484  6948 ?        Ssl  18:08   0:00 /usr/libexec/polkitd --no-debug
root      117738  0.0  0.3 392536 12772 ?        Ssl  18:08   0:00 /usr/libexec/udisks2/udisksd
root      117745  0.0  0.3 316940 12176 ?        Ssl  18:08   0:00 /usr/sbin/ModemManager

Information such as USER, PID and COMMAND of the processes are listed. To specifically search for a process, it can be written like this:

foc@ubuntu22:~$ ps -aux | grep top
foc         3000  0.0  0.0   9948  3556 pts/0    T    08:41   0:00 top
foc         3002  0.0  0.0   6476  2232 pts/0    S+   08:41   0:00 grep --color=auto top

 

Method-2: Use pidof command

After the pidof command, type the process whose pid you want to know:

foc@ubuntu22:~$ pidof top
3000

or

foc@ubuntu22:~$ pidof sshd
1007 902 695

In this way, pid(s) is learned in a fast and simple way.

Advertisement

 

Method-3: Use pgrep command

As in the pidof command, the process whose pid is to be learned in the pgrep command should be written after the pgrep command:

foc@ubuntu22:~$ pgrep top
3000

or

foc@ubuntu22:~$ pgrep bash
1008

In this way, the pid of that process are learned. Now that we have learned the pids of the processes, we can proceed to the step of killing the processes.

 

How to kill process?

There are also several alternatives for killing processes.

 

Method-1: Use pkill Command

Some processes have subprocesses. This means that multiple processes will be killed when you terminate the main process. The pkill command is used for this. You can find more detailed information about this in the article "pkill -P $$ - Kill subprocess in Linux [SOLVED]".

Usage:
pkill [options] <pattern>

Example:

foc@ubuntu22:~$ pkill -P 928

All processes are killed by giving the parent pid after the -P parameter.

 

Method-2: Use kill Command

The kill command is used to kill processes in Ubuntu. The standard usage of the kill command is:

Advertisement
kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

The kill command has kill signals that serve different purposes. Typing with the " -l" parameter lists the kill signals:

foc@ubuntu22:~$ kill -l
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
...

Some of the signals are explained as follows:

  • SIGHUP: The SIGHUP signal disconnects a process from the parent process. This an also be used to restart processes. For example, "killall -SIGUP compiz" will restart Compiz. This is useful for daemons with memory leaks.
  • SIGINT: This signal is the same as pressing ctrl-c. On some systems, "delete" + "break" sends the same signal to the process. The process is interrupted and stopped. However, the process can ignore this signal.
  • SIGKILL: The SIGKILL signal forces the process to stop executing immediately. The program cannot ignore this signal. This process does not get to clean-up either.

First find out the pid of the process:

foc@ubuntu22:~$ pidof top
2532

To kill the process, you can signal the pid with the -s parameter in the kill command:

foc@ubuntu22:~$ kill -s SIGKILL 2532

or uou can just give the signal code instead of the parameter:

foc@ubuntu22:~$ kill -SIGKILL 2532

or you can write the number corresponding to the signal code:

Advertisement
foc@ubuntu22:~$ kill -9 2532

In all 3 methods, the process will die with the kill command:

foc@ubuntu22:~$ pidof top
[1]+ Killed top

 

Method-3: Use killall Command

The killall command is used to kill processes by name. When no parameters are given, it sends a SIGTERM signal. With this command, multiple processes can be terminated simultaneously.

Usage: 
killall [OPTION]... [--] NAME..

Example:

foc@ubuntu22:~$ killall sshd

The process to be killed is killed by typing the name of the process after the killall command.

 

Summary

There is always an alternative in Linux. We have listed the processes with 3 different methods on the Ubuntu operating system. Again, we showed you how you can kill these processes with 3 different methods.

Remember you always have an alternative on Linux.

 

References

en.wikipedia.org - kill (command)
askubuntu.com - How do I kill processes in Ubuntu?

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment