The answer by @hvd is basically correct. To back that up even more, the init
process will first send SIGTERM
to processes when your are shutting down your computer, then after a delay will send SIGKILL
if they have not already exited. Processes can't handle/ignore SIGKILL
.
To give a bit more detail though, the real answer is that you have no way of knowing for sure that the program handles it. SIGTERM
is the most usual signal to use for politely asking a program to quit, but all signal handling depends on the program doing something with the signal.
To put it a different way, based on the other answers, if you had a program written by @Jos or by @AlexGreg then they would presumably be handling SIGQUIT
but possibly not SIGTERM
, and hence sending SIGTERM
would be less "soft" than SIGQUIT
.
I've written some code so you can play around with it yourself. Save the below as signal-test.c
, then compile with
gcc -o signal-test signal-test.c
You can then run it ./signal-test
, and see what happens when you send different signals with killall -s <signal>
.
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
int flag = 0;
void handle_signal(int s)
{
flag = s;
}
int main(int argc, char *argv[])
{
signal(SIGTERM, handle_signal);
signal(SIGQUIT, handle_signal);
while(flag == 0){
sleep(1);
}
printf("flag is %d\n", flag);
return flag;
}
As it stands, the code handles both SIGTERM and SIGQUIT gracefully. You could try commenting out the lines signal(SIG...
(using a //
at the start of the line) to remove the signal handler, then run and send the signals again. You should be able to see these different outputs:
$ ./signal-test
Terminated
$ ./signal-test
Quit (core dumped)
$ ./signal-test
flag is 15
$ ./signal-test
flag is 3
depending on whether you handle the signals or not.
You could also try ignoring the signals:
signal(SIGTERM, SIG_IGN);
If you do that then sending SIGTERM
will do nothing, you'll have to use SIGKILL
to end the process.
More details in man 7 signal
. Note that using signal()
in this way is considered non-portable - it's lots easier than the alternative though!
One other minor footnote - on Solaris killall
attempts to kill all processes. All of them. If you run it as root then you may be surprised :)