Five Practical Applications of the Killall command that you may not know

Source: Internet
Author: User

Linux Command lines provide many commands to kill processes. For example, you can pass a PID to the "kill" command to kill the process. The "pkill" command uses a regular expression as the input, so all processes that match the pattern are killed.

However, there is another command named "killall". By default, it precisely matches the parameter name and then kills the matching process. In this article, we will discuss the practical application of this command.

Linux killall command

The Killall command can be used to send a signal to a specific process. This signal is SIGTERM by default, but other signals can also be specified by the killall command using parameters.

Now let's take a look at the actual usage of this command through some practical examples.

1. Basic example

In this example, killall is used to kill a specific process. Suppose there are two processes with the same starting characters:

 
 
  1. $ ps -aef | grep “test” 
  2. himanshu 3969 2811 0 14:14 pts/0 00:00:00 ./test 
  3. himanshu 3970 2811 0 14:14 pts/0 00:00:00 ./test_again 

How to kill the "test_again" process?

 
 
  1. $ killall test_again 
  2. [2]+ Terminated ./test_again 

As you can see, the killall command terminates the "test_again" process. You can use the ps command to confirm this fact:

 
 
  1. $ ps -aef | grep “test” 
  2. himanshu 3969 2811 0 14:14 pts/0 00:00:00 ./test 

You can see that "test_again" is not displayed because it has been killed.

2. Use the-I option to ignore case sensitivity.

By default, the killall command is case sensitive. For example:

 
 
  1. $ ps -aef | grep “test” 
  2. himanshu 4177 3161 0 14:54 pts/3 00:00:00 ./test 
  3. himanshu 4178 3161 0 14:54 pts/3 00:00:00 ./test_again 
  4. himanshu 4180 3161 0 14:54 pts/3 00:00:00 grep --color=auto test 
  5. $ killall TEST 
  6. TEST: no process found 

You can see that the killall command cannot find the process named "TEST", but the "test" process is indeed running.

To enable the killall command to ignore case sensitivity. You can use the-I option to enable uppercase I ). For example:

 
 
  1. $ killall -I TEST 
  2. [1]- Terminated ./test 

You can see that you have successfully terminated the "test" process.

3. Use the-I option to terminate processes interactively.

The killall command can be used to terminate multiple processes.

 
 
  1. $ killall test test_again 
  2. [2]- Terminated ./test_again 
  3. [3]+ Terminated ./test 

However, if you want killall to terminate the process interactively, you can use the-I option.

Here is an example:

 
 
  1. $ killall -i test test_again 
  2. Kill test(4201) ? (y/N) y 
  3. Kill test_again(4202) ? (y/N) y 
  4. [1]- Terminated ./test 
  5. [2]+ Terminated ./test_again 

You can see that, in this way, when terminating multiple processes at the same time, the user can control whether to terminate a process.

4. Use the-q option to disable command execution echo

Sometimes, when killall cannot find the specified process, it will output an error message as follows.

 
 
  1. $ killall TEST 
  2. TEST: no process found 

However, if you want to perform killall securely, you can use the-q option:

 
 
  1. $ killall -q TEST 

As you can see, after the-q option is used, the output information of the killall command is blocked.

5. Use the-l option to list all supported signals)

As described earlier, killall sends signals to processes.

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

If you want to know all the signals that can be sent, you can use the-l option to lower case L) to obtain:

 
 
  1. $ killall -l 
  2. HUP INT QUIT ILL TRAP ABRT IOT BUS FPE KILL USR1 SEGV USR2 PIPE ALRM TERM 
  3. STKFLT CHLD CONT STOP TSTP TTIN TTOU URG XCPU XFSZ VTALRM PROF WINCH IO PWR SYS 
  4. UNUSED 

Killall supports all signals above.

Looking at the names of these signals, you may be wondering: What do these signals do?

Note: Use the following command to obtain the description of all signals:

 
 
  1. $ man 7 signal 

I need to discuss one thing with you.

The killall command man said: if the length of the process name is less than or equal to 15, the full name will be matched by default.

For example, suppose there are two processes with long names:

 
 
  1. $ ps -aef | grep “test” 
  2. himanshu 4021 3161 0 14:27 pts/3 00:00:00 ./test_abcdefghij 
  3. himanshu 4035 3161 0 14:27 pts/3 00:00:00 ./test_abcdefgh 

The name of the first process is 15 characters long. Now, let's try to use killall to kill the process:

 
 
  1. $ killall test_abcdefghij 
  2. [1]- Terminated ./test_abcdefghij 

As you can see, the killall command successfully killed the process.

If the names of both processes exceed 15 characters, killall will kill all processes according to man. For example:

 
 
  1. $ ps -aef | grep “test” 
  2. himanshu 4114 3161 0 14:40 pts/3 00:00:00 ./test_abcdefghijklmnopqrstuvwx 
  3. himanshu 4141 3161 0 14:46 pts/3 00:00:00 ./test_abcdefghijklmnopqrstuvwxyz 
  4. himanshu 4143 3161 0 14:46 pts/3 00:00:00 grep --color=auto test 

You can observe that the names of both processes are more than 15 characters long. Now, when I try to killall to kill the second process:

 
 
  1. $ killall test_abcdefghijklmnopqrstuvwxyz 
  2. [6]+ Terminated ./test_abcdefghijklmnopqrstuvwxyz 

It only kills the specified process, and the other is not killed.

 
 
  1. $ ps -aef | grep “test” 
  2. himanshu 4114 3161 0 14:40 pts/3 00:00:00 ./test_abcdefghijklmnopqrstuvwx 
  3. himanshu 4146 3161 0 14:47 pts/3 00:00:00 grep --color=auto test 

I'm not sure. Is there something wrong with my attempt or is it a bug in killall. I would be very grateful if you wrote your opinion in the comment.

By the way, this is the version of the killall command on my machine:

 
 
  1. $ killall --version 
  2. killall (PSmisc) 22.20 
  3. Copyright (C) 1993-2012 Werner Almesberger and Craig Small 
  4. PSmisc comes with ABSOLUTELY NO WARRANTY. 
  5. This is free software, and you are welcome to redistribute it under 
  6. the terms of the GNU General Public License. 
  7. For more information about these matters, see the files named COPYING. 

Via: http://linoxide.com/linux-command/linux-killall-my-options/

Translator: intermerlin Proofreader: wxy

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.