Foreword

How did you terminate your program?  This article summarizes the methods to terminate the process.

Ctrl+c

That’s right, this should be the method you use the most. After the current terminal is running a program in the background, use ctrl+c
to terminate the current program.

Ctrl+z

This method is not completely terminated, it is more like a pause, because it can continue to execute through fg or bg.
For example, the code is as follows:

//test.c

#include<stdio.h>

 

int main(void)

 

{

 

int i = 0;

 

for(i = 0;i < 100;i++)

 

{

 

printf(“%d\n”,i);

 

sleep(1);

 

}

 

return 0;

 

}

 

 

After compiling and running to a certain program, press ctrl+z:

$ gcc -o test test.c

 

$ ./test

 

After ctrl+z, type fg or bg again and you will find it continues to run the last time.

Kill

This is the most common and most commonly used, although it is called kill, but in fact it can send various types of signals to the process, of which signal 9 is more commonly used, that is, used to terminate the process.

$ kill -9 pid

Kill is used to signal the process. The above command indicates that the program sends a 9 signal to the program with the process id pid.
Common signals are as follows:

  • HUP 1 terminal disconnection
  • INT 2 interrupt (same as Ctrl + C)
  • QUIT 3 exit (same as Ctrl + \)
  • TERM 15 terminated
  • KILL 9 forced termination
  • CONT 18 continues (as opposed to STOP, fg/bg command)

It can be seen that the signal KILL is a forced termination procedure.

You can also see the meaning of these numbers by doing this:

    1. $ kill -l 9

 

    1. KILL

 

    1. $ kill -l

 

    1.  1) SIGHUP     2) SIGINT   3) SIGQUIT  4) SIGILL   5) SIGTRAP

 

    1.  6) SIGABRT     7) SIGBUS   8) SIGFPE   9) SIGKILL 10) SIGUSR1

 

    1. 11) SIGSEGV    12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM

 

 

Killall

The killall process kills the process. It will fail in some cases.
It also supports killing processes for a certain period of time, such as killing the hello process running for more than 30 minutes:

$ killall -o 30m hello

Where -o means older-than, of course you can also use -y to mean yonger-than.
m means minutes, there are other time scales:

  • s seconds
  • m minutes
  • h hours
  • d days
  • w week
  • M month
  • Year y

In addition to its name, it can also kill processes based on regular expressions. Use the -r parameter.

Pkill

Pkill can also kill processes based on the process name, which is similar to killall.

$ pkill hello

Since you may have multiple hello programs, you can do this in order to kill the oldest process:

$ pkill -o hello #oldest

Or kill the latest process:

$ pkill -n hello #newest

Or calculate the number of running hello programs:

$ pkill -c hello

How to find the process id

Some of the commands mentioned above need to be operated according to the pid. How to get the pid? There are also several common methods.

Ps

Pidof

Pidof can directly obtain the process id of a process, and it is more accurate:

$ pidof hello

 

7584

 

 

Combined with kill can be used like this:

$ kill -9 `pidof hello`

Is it all in one go?

Pgrep

It can also get the process id directly by name:

$ pgrep hello

 

19971

 

24770

 

 

Top

The process id of each process can also be seen in the results of top.

Summary

This article only briefly introduces some common termination process methods. For more usage, refer to the corresponding manual.
But these operations all have the same essence, that is, to send a signal to the process, and the process usually processes the signal to terminate the program or be forced to terminate. So if you want to achieve elegant exit , you can try to send a specific signal to the process, after receiving the signal, the process will do the aftermath, and then exit.