Terminating Processes
Learn how to terminate a process.
We'll cover the following...
Managing tasks from the command line
Every so often, a program misbehaves. Sometimes, it’s due to something we configured incorrectly. Other times, it’s because the program has a bug. We’ve probably experienced a stuck program before and figured out how to kill the task using a graphical task manager. We can manage tasks from the command-line interface as well. All we need is the process ID.
Let’s start a long-running task in the background. In our terminal, we execute the following command:
sleep 9999999 &
This process is now running. We use the jobs -l
command to list the running background jobs and show the process ID in addition to the job ID:
jobs -l
The kill
command
The kill
command terminates processes by sending the process a signal. A well-behaved program listens for these signals and responds appropriately. We specify the kill
command to terminate a process, followed by the signal we want to send, followed by the process ID.
The SIGHUP
signal, which means “hang up,” is the disconnection signal. It’s the signal that gets sent when we press “Ctrl+d” or when we close a terminal session. Programs have to be designed to respond to this signal and can choose to ignore it.
Let’s use kill -SIGHUP
followed by the process ID to send the signal to the program:
kill -SIGHUP {process ID}
jobs -l
Execute the last two commands in the terminal below:
The nohup
utility program
Sometimes, the program doesn’t respond to the hangup signal, so we have to be a little more aggressive. Let’s use the nohup
program to start this job. The ...