Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Eight uses of using killall command to terminate a process under Linux

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

The Linux command line provides many commands to kill processes. For example, you can kill processes by passing a PID to the kill command; the pkill command takes 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 will send a SIGTERM signal to a/group process, but you can also send a specified signal via parameters.

Here are eight examples of how to use killall.

1. basic usage

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

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.

The remaining hello2 and hello3 processes, we want to kill them at once, that is, kill the process in bulk, 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 begin 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 similarly, we can kill all processes run by a user.

For example, user harry is now running 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

We now want to kill all processes Harry runs, which can be done as follows:

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 with caution because it will kill all processes of the user, including terminal processes, and will cause the user to exit directly. So don't try this option lightly if you don't want to get beat up.

3. Finally the way time terminates the process

If we are running a lot of programs and we only want to kill processes that run longer than 5 hours, 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 run, you can use the-y option, where y stands for younger, as follows:

killall -y 4h

These two options are also very rude and will also exit the terminal, so I won't demonstrate it 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 correctly.

[alvin@VM_0_16_centos test]$ killall HELLO1

TEST1: no process found

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 ./ hello1

5. Close command execution echo

By default, killall will tell 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]$

6. List all supported signals

As mentioned earlier, the killall command sends SIGTERM signals by default, so can Ann send other signals? Of course it's okay. 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

UNUSED

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

7. interactive operation

If you are worried about killing multiple processes and are worried about killing processes that should not be killed, you can use the-i option to freely decide which processes should be killed and which 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 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 ./ hello1

There seems to be no effect here, but when actually executed, you can find that the execution result will appear in one or two seconds, and without the-w option, the execution result will be displayed immediately.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 237

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report