kill stopped jobs in linux

How to Kill Stopped Jobs in Linux

In Linux, all processes and commands are treated as jobs started and managed by the shell. Each job has an ID, command to be executed, start time and other attributes useful for identification and monitoring. Scripts, redirections, executables, binaries, etc. all are run as jobs. Each Linux job can be either run in foreground (meaning terminal) or background. Sometimes you may need to kill stopped jobs in Linux. In this article, we will learn how to kill stopped jobs in Linux. In this article, we will learn how to kill stopped jobs in Linux.

Foreground jobs run on terminal till your session ends or the job terminates. While a job is running in foreground, you will not be able to run another command in terminal using same session. You need to start another session to run further commands.

Background jobs are jobs that run in the background, not terminal. You can run a job in foreground by simply adding ampersand(&) after the command/script. Once a job is delegated to background, you will immediately see the shell prompt again. Long running commands are generally run in background to free the terminal for running other processes. When you assign a job to run in background, the shell will immediately display a number in square brackets []. It is the Job’s PID for future reference.

How to Kill Stopped Jobs in Linux

You can use jobs command to get a list of all jobs in your system. Here is a sample output.

$ sudo jobs
[1]- Running firefox &
[2]+ Done    nautilus

In the above output, the numbers within square brackets are the serial numbers for the jobs. Jobs are enqueued with serial number assigned for each job. The minus sign indicates the next jobs while plus sign indicates current job. Next, it shows the state of the process as Running, Stopped, Done or Exit with exit code. Lastly, it shows the actual command used to start the job.

If you also want to view the PID of each job, you need to use -l option.

$ jobs -l
[1]- 4142 Running firefox &
[2]+ 4120 Done    nautilus

The jobs command offers various useful options.

  • -n – Shows jobs that have changed their status since the last notification
  • -p – Lists only PIDs of jobs.
  • -r –Shows running jobs only
  • -s – Shows only stopped jobs.

Terminate or Kill Jobs

You can kill jobs using kill command using an identifier to identify one or more jobs that need to be terminated. This can be the PIDs, substring.

Here is a command to kill jobs with specific PIDs.

$ kill -9 3454 2333 4546

Here is the command to kill current running job.

$ kill %%
$ kill %+

If you want to kill a job using a substring of its command, here it the command for it. In this case, you need to prefix substring with %?.

$ kill %?gnome-calculator

However, please note, if you terminate a terminal session, all jobs running in it will be terminated.

How to Kill Stopped Jobs

As we have seen earlier, yo can use jobs command to list jobs based on their current status. Here is the command to list all stopped jobs.

$ jobs -s

We also know that using -p option will list only PIDs of jobs. So you can use both options together to get a list of PIDs of all stopped jobs.

$ jobs -p -s

Once you have a list of PIDs of all stopped jobs in your system, you can pass them to kill command to terminate them. We use kill -9 command to send kill signal to the stopped processes.

$ sudo kill -9 `jobs -p -s`

You can modify the above command to find and kill other processes based on their current status. For example, here is the command to kill all running processes. Here we use -r option in jobs command, instead of using -s option that we did earlier.

$ sudo kill -9 `jobs -p -r`

In this article, we have learnt how to kill stopped processes in Linux.

Also read:

How to Change MAC Address in Linux
How to Convert Permissions to Octal in Linux
How to Compare Local & Remote Files in Linux
How to Generate Strong Pre Shared Key in Linux
How to Run Command After Certain Time in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *