How Do You Manage (List, Resume or Terminate) Suspended Linux Commands?

Problem scenario
You have started some commands then used Ctrl-z while they were still processing.  You want to see which jobs have been suspended.  You may want to terminate or run the jobs. What do you do?

Solution
Use the builtin Linux command "jobs" like this:

jobs -l

# Sample output may look like this:

[1]- 26830 Stopped                 ping -c 100 8.8.8.8
[2]+ 26831 Stopped                 ping -c 99 8.8.8.8

To resume the lowest job in the output of the above command, use this command with no arguments:

fg

# fg will resume the job with the "+" sign in the list from the "jobs -l" command.
# fg can be used to start specific jobs and not necessarily the bottom job when it has the correct argument.  

To terminate a job listed in "jobs -l" you can use this command:

disown <pid> # where <pid> is the number listed before the word "Stopped" in the output of "jobs -l"

You can also refer to the numbers such as [1] and [2] as they appear in the left hand column in the output of "jobs -l".  To do this use the %x format where "x" is the number that is in the brackets "[]" in the left hand column of the output of "jobs -l".

You can use fg or bg to resume the suspended jobs.  Based on this integer in the brackets "[]" in the left hand column of the output of "jobs -l", you can use the % syntax like this:

fg %1  #this will resume the "ping -c 100 8.8.8.8" job in the foreground

or

bg %1 #this will resume the "ping -c 100 8.8.8.8" job in the background

# The disown command is a builtin command. It can also refer to jobs with the %x syntax like the fg and bg examples above.

Leave a comment

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