Linux with the killall command to terminate a process in 8 usage examples
- 2021-01-18 06:55:51
- OfStack
Linux's command line provides a number of commands to kill processes. For example, you can pass an PID to the kill life to kill the process; The pkill command takes a regular expression as input, so any process that matches this pattern is 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 use of this command.
By default, the killall command will send 1 SIGTERM signal to 1 / group of processes, but it is possible to send 1 specified signal as an argument.
Let's take a closer look at the 8 uses of killall by example.
1. Basic usage
ES19en1, ES20en2, ES21en3, ES19en1, ES20en2, ES21en3, ES22en1, ES19en1, ES20en2, ES21en3, ES22en1, ES19en1, ES20en2, ES21en3
killall hello1
The results of the run 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
As you can see, the hello1 process has been killed.
For the rest of the ES34en2 and ES35en3 processes, we would like to kill them once, i.e. in batch, 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 ./hello3
Thus, processes starting with hello are all killed.
2. Terminate a process that a user is running
We can kill a group of processes to satisfy a regular expression, and likewise we can kill all processes run by a user.
For example, user harry now runs the following 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
We now want to kill all the processes that harry is running by doing the following:
killall -u harry
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 0.0 112648 964 pts/0 R+ 14:58 0:00 grep --color=auto harry
However, this option should be used with caution, as it will kill all of the user's processes, including terminal processes, which will cause the user to exit directly. So don't try this option if you don't want to get punched.
3, finally time way to terminate the process
If we are running a lot of programs now and we only want to kill processes running longer than 5h, we can use the -o option, where o stands for older as follows:
killall -o 5h
Similarly, if you want to kill processes that take less than 4h, you can use the -y option, where y stands for younger, as follows:
killall -y 4h
These two options are also very crude and will also exit the terminal, so I won't demonstrate them for now.
4. Ignoring case
By default, the killall command is case-sensitive, so we will not kill the process properly if we miswrite case.
[alvin@VM_0_16_centos test]$ killall HELLO1
TEST1: no process found
If we want to ignore case, we can add the -I (uppercase i) option.
[alvin@VM_0_16_centos test]$ killall -I HELLO1
[1] Terminated ./hello1
5. Close the command execution echo
By default, killall will tell you the execution of the command, but what if we don't care about the result and just want it to execute silently? Simply add the -q option, where q denotes 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, by default, the killall command will send the SIGTERM signal. Can Ann send other signals? Of course you can. You can view all the signals supported by killall using the -l option:
[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 worried about killing multiple processes, and you're worried about killing the wrong ones, you can use the -i option, which gives you the freedom to decide which processes to kill and which to keep.
[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
Wait until a process is terminated
When a signal is sent to a process, you can use the -w option if you want to confirm that the process has been killed before returning the execution result, where w stands for wait, as follows:
[alvin@VM_0_16_centos test]$ killall -w hello1
[4]+ Terminated ./hello1
It doesn't look like much, but when it's actually executed, the result will appear in a second or two. Without the -w option, the result will appear immediately.
conclusion