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

How to use the killall command to terminate a process in Linux

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

How to use the killall command in Linux to terminate the process, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

1. Basic usage

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

Killall hello1

The result of the operation is as follows:

[alvin@VM_0_16_centos test] $ps aux | grep hello alvin 12061 0.0 4152 344 pts/0 S 14:41 0:00. / hello1 alvin 12074 0.0 4152 344 pts/0 S 14:41 0:00. / hello2 alvin 12084 0.0 4152 340 pts/0 S 14:41 0:00. / hello3 alvin 12089 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 4152 344 pts/0 S 14:41 0:00. / hello2 alvin 12084 0.0 4152 340 pts/0 S 14:41 0: 00. / hello3 alvin 12170 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 remaining hello2 and hello3 processes, we want to kill them all at once, that is, to kill them in batches. You can do 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

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

2. Terminate the process run by a user

We can kill a set of processes that satisfy a regular expression, just as we can kill all processes that a user is running.

For example, user harry is now running 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.00.1 116436 3252 pts/1 Ss+ 14:55 0:00-bash harry 13948 0.0 4152 3252 pts/1 S 14:57 0:00. / hello1 harry 13952 0.0 4152 344 pts/1 S 14:57 0:00. / hello2 harry 13959 0.00.04152 344 pts/1 S 14:57 0:00. / hello3 alvin 14005 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, and we can do this as follows:

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 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 the 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. Terminating the process by the way of final time

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

Killall-o 5h

Similarly, if you want to kill processes that take less than 4 hours, 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 I won't demonstrate it for a while.

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 (uppercase I) option.

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

5. Turn off the echo of command execution

By default, killall tells you about the execution of the command, but what if we don't care about the result of its execution and just want it to execute silently? Simply add the-Q option, where Q represents 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 SIGTERM signals, so can Ann send other signals? Of course you can. You can use the-l option to view all the 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 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 and are worried about killing processes that should not be killed, you can use the-I option so that you are free to decide which processes should be killed and which processes should be retained.

[alvin@VM_0_16_centos test] $killall-I hello* Kill hello2 (13825)? Y Kill hello3 (13831)? 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's answer to the question on how to use the killall command to terminate the process in Linux is shared here. I hope the above content can be of some help to everyone. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

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: 0

*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