Eight main uses of killall commands to terminate processes on Linux

Keywords: Programming Linux REST openssh sftp

Linux's command line provides many commands to kill a process.For example, you can pass a PID to kill a process; the pkill command uses a regular expression as input, so processes matching that pattern are killed.

But there is also a command called killall, which by default matches the parameter name exactly and then kills the matching process.In this article, we will discuss the practical application of this command.

By default, the killall command sends a SIGTERM signal to a/group process, but it can also send a specified signal through a parameter.

Here are eight main uses of killall in detail through examples.

1. Basic Usage

If we have three processes running, hello1, hello2, hello3, and now we want to kill the hello1 process, we can use the following directly:

  1. killall hello1 

The results are as follows:

  1. [alvin@VM_0_16_centos test]$ ps aux | grep hello  
  2. alvin    12061  0.0  0.0   4152   344 pts/0    S    14:41   0:00 ./hello1  
  3. alvin    12074  0.0  0.0   4152   344 pts/0    S    14:41   0:00 ./hello2  
  4. alvin    12084  0.0  0.0   4152   340 pts/0    S    14:41   0:00 ./hello3  
  5. alvin    12089  0.0  0.0 112648   964 pts/0    R+   14:41   0:00 grep --color=auto hello  
  6. [alvin@VM_0_16_centos test]$ killall hello1  
  7. [1]   Terminated              ./hello1  
  8. [alvin@VM_0_16_centos test]$ ps aux | grep hello  
  9. alvin    12074  0.0  0.0   4152   344 pts/0    S    14:41   0:00 ./hello2  
  10. alvin    12084  0.0  0.0   4152   340 pts/0    S    14:41   0:00 ./hello3  
  11. alvin    12170  0.0  0.0 112648   964 pts/0    R+   14:42   0:00 grep --color=auto hello 

You can see that the hello1 process has been killed.

The rest of the hello2 and hello3 processes that we want to kill at once, or in batch, can be as follows:

  1. [alvin@VM_0_16_centos test]$ killall hello*  
  2. hello: no process found  
  3. hello1: no process found  
  4. hello.c: no process found  
  5. [2]-  Terminated              ./hello2  
  6. [3]+  Terminated              ./hello3 

In this way, all processes that start with hello are killed.

2. Terminate a process run by a user

We can kill a set of processes that satisfy a regular expression, and in the same way, we can kill all the processes that a user runs.

For example, user harry now runs several processes:

  1. [alvin@VM_0_16_centos test]$ ps aux | grep harry  
  2. root     13675  0.0  0.2 148236  5584 ?        Ss   14:55   0:00 sshd: harry [priv]  
  3. harry    13677  0.0  0.1 148236  2944 ?        S    14:55   0:00 sshd: harry@pts/1  
  4. root     13678  0.0  0.2 148236  5444 ?        Ss   14:55   0:00 sshd: harry [priv]  
  5. harry    13680  0.0  0.1 148236  2252 ?        S    14:55   0:00 sshd: harry@notty  
  6. harry    13681  0.0  0.1  53228  2168 ?        Ss   14:55   0:00 /usr/libexec/openssh/sftp-server  
  7. harry    13694  0.0  0.1 116436  3252 pts/1    Ss+  14:55   0:00 -bash  
  8. harry    13948  0.0  0.0   4152   344 pts/1    S    14:57   0:00 ./hello1  
  9. harry    13952  0.0  0.0   4152   344 pts/1    S    14:57   0:00 ./hello2  
  10. harry    13959  0.0  0.0   4152   344 pts/1    S    14:57   0:00 ./hello3  
  11. alvin    14005  0.0  0.0 112648   964 pts/0    R+   14:58   0:00 grep --color=auto harry 

Now we want to kill all the processes that harry runs by doing the following:

  1. killall -u harry 

The results are as follows:

  1. [alvin@VM_0_16_centos test]$ sudo killall -u harry  
  2. [alvin@VM_0_16_centos test]$ ps aux | grep harry  
  3. alvin    14040  0.0  0.0 112648   964 pts/0    R+   14:58   0:00 grep --color=auto harry 

However, this option should be used cautiously because it will kill all processes of the user, including terminal processes, and cause the user to exit directly.So don't try this option lightly if you don't want to be beaten.

3. Terminate process at last time

If we're running a lot of programs now and we just want to kill processes that run for more than 5 hours, then we can use the -o option, where O stands for older as follows:

  1. killall -o 5h 

Similarly, if you want to kill a process that takes less than 4 hours to complete, you can use the -y option, where y stands for younger, as follows:

  1. killall -y 4h 

These two options are also very rough and will exit the terminal, so don't show them first.

4. Ignore case

By default, the killall command is case sensitive, so if we write the wrong case, we won't be able to kill the process properly.

  1. [alvin@VM_0_16_centos test]$ killall HELLO1  
  2. TEST1: no process found 

If we want to ignore case, we can add the -I (capital letter i) option.

  1. [alvin@VM_0_16_centos test]$ killall -I HELLO1  
  2. [1]   Terminated              ./hello1 

5. Turn off command execution echo

By default, killall tells you how the command was executed, but what if we don't care about its results and just want it to execute silently?Just add the -q option, where Q means quite, as follows:

  1. [alvin@VM_0_16_centos test]$ killall HELLO2  
  2. HELLO2: no process found  
  3. [alvin@VM_0_16_centos test]$ killall -q HELLO2  
  4. [alvin@VM_0_16_centos test]$ 

6. List all supported signals

As mentioned earlier, killall command will send SIGTERM signal by default, can Ann send other signal?Yes, of course.You can use the -l option to view all the signals that killall supports:

  1. [alvin@VM_0_16_centos test]$ killall -l  
  2. HUP INT QUIT ILL TRAP ABRT IOT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM  
  3. STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH IO PWR SYS  
  4. UNUSED 

You can use the -s option (followed by a signal name) to send a special signal to a process.

7. Interactive operation

If you're not reassured that you're killing processes that you shouldn't have killed, you can use the -i option, which gives you the freedom to decide which ones should be killed and which ones should be preserved.

  1. [alvin@VM_0_16_centos test]$ killall -i hello*  
  2. Kill hello2(13825) ? (y/N) y  
  3. Kill hello3(13831) ? (y/N) N  
  4. hello: no process found  
  5. hello1: no process found  
  6. hello3: no process found  
  7. hello.c: no process found  
  8. [2]-  Terminated              ./hello2 

8. Wait until a process is terminated

When a signal is sent to a process, if you want to make sure that the process has been killed before returning the execution results, you can use the -w option, where w stands for wait, as follows:

  1. [alvin@VM_0_16_centos test]$ killall -w hello1  
  2. [4]+  Terminated              ./hello1 

It doesn't seem to have any effect here, but when you actually execute, you can see that the result of execution will appear in a second or two. Without the -w option, the result will be displayed immediately.

Feeling good, please give me some support. Welcome to leave a message or join my personal group 855801563 to receive [Architecture Data Subject Collection 90], [BATJTMD JAVA Interview True Question 1000+]. This group is dedicated to learning communication technology, sharing interview opportunities, rejecting advertisements, and I will also answer questions and discuss them regularly in the group.

Posted by RamboJustRambo on Tue, 07 May 2019 16:00:39 -0700