Intro To 'kill' Command In Linux
2024-01-31 - By Robert Elder
I use the 'kill' command to kill a running process:
kill 66042
Example Use Case Of 'kill' Command
Here, I have a short video of my favourite cat:
When I click on the video file, it will open in VLC media player, except for when it doesn't. For some reason, the VLC media player process will sometimes get stuck running in the background. When this happens, it refuses to open anymore video files.
I can use the 'ps' command to find the process ID of the stuck VLC media player process, and then use the 'kill' command to force the process to end:
ps -ef | grep vlc
robert 77746 2999 2 16:29 ? 00:00:01 /usr/bin/vlc --started-from-file /cool-videos/favourite-cat.mp4
robert 77917 65790 0 16:30 pts/1 00:00:00 grep --color=auto vlc
kill -s SIGKILL 77746
Now, I can watch the video of my cat again.
Listing Signal Types
There are many different types of signals that you may want to send to a process. The most common ones are 'SIGTERM' or 'SIGKILL'. You can use the '-l' flag to see a list of different signals that you can send to a process:
kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
SIGTERM Vs. SIGKILL
Here, I've written a busy loop program that catches the SIGTERM signal:
#include <signal.h>
#include <stdio.h>
static volatile int isRunning = 1;
void sighupHandler(int dummy) {
isRunning = 0;
}
int main(){
signal(SIGTERM, sighupHandler);
while(isRunning) { }
printf("SIGTERM caught, exiting gracefully...\n");
return 0;
}
If I compile and run this program
gcc sigterm.c -o sigterm
./sigterm
and then send the process a SIGTERM signal:
ps -ef | grep sigterm
robert 67710 67389 99 16:02 pts/2 00:00:13 ./sigterm
robert 67737 67459 0 16:02 pts/3 00:00:00 grep --color=auto sigterm
kill -s SIGTERM 67710
the signal handler in my program will catch the SIGTERM signal and exit the program gracefully:
SIGTERM caught, exiting gracefully...
However, if I run the program again:
./sigterm
but send a SIGKILL signal instead:
ps -ef | grep sigterm
robert 67767 67389 97 16:02 pts/2 00:00:10 ./sigterm
robert 67771 67459 0 16:02 pts/3 00:00:00 grep --color=auto sigterm
kill -s SIGKILL 67767
it will not catch the signal and exit the program abruptly without being given a chance to shut down:
Killed
And that's why the 'kill' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|