The Linux command line provides many commands to kill processes. For example, you can pass a PID to the kill to kill the process; The pkill command takes a regular expression as input, so processes that match the 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 is also possible to send a specified signal with a parameter.

Here are eight uses of killall.

1. Basic usage

Hello1: hello2: hello3: hello1: hello2: hello3: hello1: hello2: hello3: hello1: hello2: hello3: hello1: hello2: hello3

killall hello1Copy the code

The results are as follows:

[alvin@VM_0_16_centos test] $ps aux | grep hello alvin 12061 0.0 0.0 4152 344 PTS / 0 S he cometh 0:00. / hello1 alvin 12074 0.0 0.0 4152 344 PTS / 0 S 14:41 0:00./hello2 alvin 12084 0.0 0.0 4152 340 PTS /0 S 14:41 0:00./hello3 alvin 12089 0.0 0.0 112648 964 PTS /0 R+ 14:41 0:00 grep --color=auto hello [alvin@VM_0_16_centostest]$ killall hello1
[1]   Terminated              ./hello1
[alvin@VM_0_16_centos test] $ps aux | grep hello alvin 12074 0.0 0.0 4152 344 PTS / 0 S he cometh 0:00. / hello2 alvin 12084 0.0 0.0 4152 340 PTS / 0 S 14:41 0:00./hello3 alvin 12170 0.0 0.0 112648 964 PTS /0 R+ 14:42 0:00 grep --color=auto helloCopy the code

As you can see, the Hello1 process has been killed.

For the rest of the hello2 and Hello3 processes, we want to kill them all at once, i.e. in batches, by doing the following:

[alvin@VM_0_16_centos test]$ killall hello*
hello: no process found
hello1: no process found
hello.c: no process found
[2]-  Terminated              ./hello2
[3]+  Terminated              ./hello3Copy the code

Thus, all processes that begin with Hello are killed.

2. Terminate a process running by a user

Just as we can kill a set of processes that satisfy a regular expression, we can also kill all processes that a user runs.

For example, user Harry is running the following processes:

[alvin@VM_0_16_centos test] $ps aux | grep harry root 13675 148236 5584 0.0 0.2? Ss 14:55 0:00 SSHD: Harry [priv] Harry 13677 0.0 0.1 148236 2944? S 14:55 0:00 SSHD: harry@pts/1 root 13678 0.0 0.2 148236 5444? Ss 14:55 0:00 SSHD: Harry [priv] Harry 13680 0.0 0.1 148236 2252? S 14:55 0:00 SSHD: harry@notty Harry 13681 0.0 0.1 53228 2168? Ss 14:55 0:00 / usr/libexec/openssh/SFTP server harry - 13694 116436 3252 PTS/Ss 1 + 0.0 0.1 14:55 0:00 - bash harry 13948 0.0 0.0 4152 344 PTS /1 S 14:57 0:00./hello1 Harry 13952 0.0 0.0 4152 344 PTS /1 S 14:57 0:00./hello2 Harry 13959 0.0 0.0 4152 344 PTS /1 S 14:57 0:00./hello3 alvin 14005 0.0 0.0 112648 964 PTS /0 R+ 14:58 0:00 grep --color=auto harryCopy the code

We now want to kill all the processes harry is running by doing this:

killall -u harryCopy the code

The running results are as follows:

[alvin@VM_0_16_centos test]$ sudo killall -u harry
[alvin@VM_0_16_centos test] $ps aux | grep harry alvin 14040 0.0 112648 0.0 964 PTS / 0 R + 14:58 0:00 grep -- color = auto harryCopy the code

However, use this option with caution because it will kill all processes of the user, including terminal processes, causing the user to exit directly. So don’t try this option if you don’t want to get beat up.

3. Terminate the process in time mode

If we are running a lot of programs now and we only want to kill processes that have been running for more than 5h, we can use the -o option, where o stands for older as follows:

killall -o 5hCopy the code

Similarly, if you want to kill processes that have been going on for less than 4h, use the -y option, where y stands for Younger, as follows:

killall -y 4hCopy the code

These two options are also very rude and will also exit the terminal, so I won’t show you.

4. Ignore case

By default, the killall command is case sensitive, so we can’t kill the process correctly if we write the wrong case.

[alvin@VM_0_16_centos test]$ killall HELLO1
TEST1: no process foundCopy the code

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

[alvin@VM_0_16_centos test]$ killall -I HELLO1
[1]   Terminated              ./hello1Copy the code

5. Close the command output

By default, killall tells you how the command is executed, but what if we don’t care about its execution and just want it to execute silently? Simply add the -q option, where Q stands for quite, as follows:

[alvin@VM_0_16_centos test]$ killall HELLO2
HELLO2: no process found
[alvin@VM_0_16_centos test]$ killall -q HELLO2
[alvin@VM_0_16_centos test] $Copy the code

6. List all supported signals

As mentioned earlier, the killall command sends SIGTERM signals by default, so can Ann send other signals? Of course you can. You can use the -l option to view all signals supported by Killall:

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

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

7. Interactive operation

If you are worried about killing multiple processes, you can use the -i option, which gives you the freedom to decide which processes should be killed and which should be kept.

[alvin@VM_0_16_centos test]$ killall -i hello*
Kill hello2(13825) ? (y/N) y
Kill hello3(13831) ? (y/N) N
hello: no process found
hello1: no process found
hello3: no process found
hello.c: no process found
[2]-  Terminated              ./hello2Copy the code

8. Wait until a process is terminated

When a signal is sent to a process and you want to confirm that the process has been killed before returning the result, you can use the -w option, where w stands for wait, as follows:

[alvin@VM_0_16_centos test]$ killall -w hello1
[4]+  Terminated              ./hello1Copy the code

This doesn’t look like much, but when you actually execute it, you can see that the execution results appear after a second or two, rather than immediately without the -w option.

Code word is not easy, if you think it is helpful, please click a “like” and then go ~

—————–

Wechat search public number [Liangxu Linux], background reply keywords:

  1. Re [1024] : Free access to constantly updated massive technical resources;

  2. Reply [CSDN] : Free access to CSDN resources download group;

  3. Reply [into group] : free access to master such as cloud technology exchange group.

Dry goods sharing, Resource sharing, cognitive improvement, English learning

Code word is not easy, if you think it is helpful, please click a “like” and then go ~

—————–

I am good xu, the world’s top 500 foreign companies Linux development engineer, specializing in the production of Linux dry goods. Welcome to pay attention to my public number “liang Xu Linux”, reply “1024” to obtain the latest and most complete technical information, reply “into the group” into the master such as cloud technology exchange group.