UNDERSTANDING THE SUBJECT MATTER
Having understood what a process is and how to manage them in managing process in Linux part 1, it is high time we learned how to kill a process in Linux and what a Job is also. This subject matter will be a sequel to part 1.
Killing a process in Linux has to do with sending signals to the process.
There are some situations where an application will stop responding and you may have done all the necessary things to stop and start the application but it never comes up because the real process is still running and was never stopped in the real sense, the only way to come out from such sometimes is to kill the process.
There are many types of signals available in Linux that you can send to processes. Let’s check the man page of signals to understand what some of these signals are.
man 7 signal
This subject matter will only focus on the SIGKILL and the SIGTERM signals.
To kill a process in Linux, you will have to send signals such as the sigterm or the sigkill signal to the process with the use of the “kill” utility.
Signals can be sent to a process by using their signal names or their corresponding signal values as seen in the screenshot above.
Other signals such as SIGSTOP, SIGCONT, SIGHUP, etc can also be sent to a process. The sigstop signal from its name will stop a process, the sigcont signal from its name will continue/resume the stopped process and the sighup signal will restart a process.
Using the short cut key (ctrl+z) is the same as using the SIGSTOP signal. You will understand how the short cut key (ctrl+z) can be used as we go on in this subject matter.
The sigterm signal kills a process gracefully while the sigkill signal kills a process abruptly. Using the short cut key (ctrl+c) is the same as using the sigterm signal.
There are some processes you will send the sigterm signal to and it won’t kill the process, hence, you will be left with no option than to use the sigkill signal.
More so, you need to be careful while using the sigkill signal because some processes may be doing a critical job at the time you use the sigkill signal, which may corrupt the jobs.
The sigkill signal should not be used at all times to kill a process and should be avoided if it can be, especially on critical processes
To Kill a process with the sigterm signal using the signal value, use the command,
kill <PID>
One doesn’t necessarily need to specify the signal value when using the sigterm signal. If a value is not specified, the system will assume the sigterm signal by default.
To kill a process with the sigterm signal using the signal name, use the command,
kill -SIGTERM <PID>
To kill a process with the sigkill signal using the sigkill value, use the command,
kill -9 <PID>
To kill a process with the sigkill signal using the signal name, use the command,
kill -SIGKILL <PID>
We will look at using these commands with examples in the “ACTION TIME” section.
A process can also be killed by name instead of using the process ID. This comes in handy when you have a lot of processes running with the same, you do not have to kill them one after the other, you can kill them all at once.
To kill a process by the process name, use the command,
pkill -9 <process-name>
OR
killall -9 <process-name>
Please see examples in the “ACTION TIME” section.
Going forward, I mentioned a job in part 1 of this lesson, let us understand what a job is.
A Job in Linux is a program that is not running as a daemon (i.e does not run as a background process).
A Job is started interactively on the shell. For example, the command “ls”, “mv”, “cp”, etc are all jobs. They do not run in the background as a process. Though they are processes logically because everything that runs on the system runs as a process.
A job cannot be made persistent after a reboot. It just runs on the Linux shell.
There are some jobs that keep the shell busy for a while, thereby making the admin redundant until the job gets completed.
The admin cannot continue their work except another terminal is opened. One of the Jobs that take a long time is using the dd utility to make a flash boo-table, especially an ISO that is about 8 gig. I know how much time this takes me.
Let’s make a dummy command using the dd utility for example,
[root@lab02 ~]# dd if=/dev/zero of=/dev/null
you can see that the command keeps the shell busy and won’t release the cursor. As a user, you can make this kind of job run in the background so that you can continue to do other things.
To make a job that keeps a shell busy run in the background, enter the key, ctrl+z to stop/suspend the job, then enter the command, “bg” immediately to make the job run in the background.
[root@lab02 ~]# dd if=/dev/zero of=/dev/null
^Z
[1]+ Stopped dd if=/dev/zero of=/dev/null
[root@lab02 ~]#
[root@lab02 ~]# bg
[1]+ dd if=/dev/zero of=/dev/null &
Also, when you know that a shell job you want to run will keep your shell busy for a while, you can run the command straight away by putting the job in the background
To run a job in the background straight away, use the ampersand character after the command. For example, use the command
[root@lab02 ~]# dd if=/dev/zero of=/dev/null &
[1] 93196
[root@lab02 ~]#
[root@lab02 ~]# jobs
[1]+ Running dd if=/dev/zero of=/dev/null &
[root@lab02 ~]#
To verify the background running jobs, use the command,
[root@lab02 ~]# jobs
[1]+ Running dd if=/dev/zero of=/dev/null &
One thing you must know is that when you put a job in the background process, it keeps queuing until they are done.
[root@lab02 ~]# jobs
[1] Running dd if=/dev/zero of=/dev/null &
[2] Running dd if=/dev/zero of=/dev/null &
[3]- Running dd if=/dev/zero of=/dev/null &
[4]+ Running dd if=/dev/zero of=/dev/null &
To bring a job back from the background to the foreground, use the command,
[root@lab02 ~]# fg
dd if=/dev/zero of=/dev/null
Also, If there are more than one background jobs running, you can also bring any background job to the foreground by using the command,
fg 1, fg 2, fg n, where n is the number of the background process.
for example, from the background jobs below,
[root@lab02 ~]# jobs
[1] Running dd if=/dev/zero of=/dev/null &
[2] Running dd if=/dev/zero of=/dev/null &
[3]- Running dd if=/dev/zero of=/dev/null &
[4]+ Running dd if=/dev/zero of=/dev/null &
[root@lab02 ~]#
To bring back the no 4 jobs to the foreground, use the command,
[root@lab02 ~]# fg 4
dd if=/dev/zero of=/dev/null
This is also applicable to putting a stopped job in the background. For example, if the stopped job has a job ID of 4, to put it in the background, use the command,
[root@lab02 ~]# bg 4
To terminate a job, enter the key, ctrl+c
[root@lab02 ~]# fg
dd if=/dev/zero of=/dev/null
^C67967322+0 records in
67967321+0 records out
34799268352 bytes (35 GB, 32 GiB) copied, 354.649 s, 98.1 MB/s
[root@lab02 ~]#
You can also terminate a job by sending a signal. For example, if a job ID is 4 and it is running in the background, you can use the command,
[root@lab02 ~]# kill -SIGTERM %4
ACTION TIME
1. To Kill the vsftpd process with PID, do the following.
- get the process ID
[root@lab02 ~]# pidof vsftpd
36460
[root@lab02 ~]#
- Kill the process with the sigterm or sigkill signal.
[root@lab02 ~]# kill 36460
[root@lab02 ~]#
OR
[root@lab02 ~]# kill -9 36460
2. To Kill the vsftpd process with process name, use the command,
[root@lab02 ~]# pkill vsftpd
[root@lab02 ~]#
3. To kill A Process That Has Child Processes, for example httpd process, do the following
- get the process id
[root@lab02 ~]# pidof httpd
70239 36809 36808 36807 36806 36805
[root@lab02 ~]#
- kill the process
[root@lab02 ~]# killall httpd
[root@lab02 ~]#
4. To Kill Many Process At The Same Time, use the command,
[root@lab02 ~]# kill 24353 62562 67278
Hi, many tops here are still talking about redhat 7. For e.g the man 7 signal is entirely different. Likewise many other topics. How often is update done on ur website?