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:
- killall hello1
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 14:41 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_centos test]$ 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 14:41 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 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:
- [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 ./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:
- [alvin@VM_0_16_centos test]$ ps aux | grep harry
- root 13675 0.0 0.2 148236 5584 ? 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 0.0 0.1 116436 3252 pts/1 Ss+ 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 harry
Now we want to kill all the processes that harry runs by doing the following:
- killall -u harry
The 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 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:
- 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:
- 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.
- [alvin@VM_0_16_centos test]$ killall HELLO1
- TEST1: no process found
If we want to ignore case, we can add the -I (capital letter i) option.
- [alvin@VM_0_16_centos test]$ killall -I HELLO1
- [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:
- [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]$
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:
- [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
- 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.
- [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 ./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:
- [alvin@VM_0_16_centos test]$ killall -w hello1
- [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.