In this quick tutorial, I want to look at the jobs
command and a few of the ways that we can manipulate the jobs running on our systems. In short, controlling jobs lets you suspend and resume processes started in your Linux shell.
Jobs
The jobs
command will list all jobs on the system; active, stopped, or otherwise. Before I explore the command and output, I'll create a job on my system.
I will use the sleep
job as it won't change my system in any meaningful way.
[tcarrigan@rhel ~]$ sleep 500
^Z
[1]+ Stopped sleep 500
First, I issued the sleep
command, and then I received the Job number [1]. I then immediately stopped the job by using Ctl+Z. Next, I run the jobs
command to view the newly created job:
[tcarrigan@rhel ~]$ jobs
[1]+ Stopped sleep 500
You can see that I have a single stopped job identified by the job number [1].
Other options to know for this command include:
- -l - list PIDs in addition to default info
- -n - list only processes that have changed since the last notification
- -p - list PIDs only
- -r - show only running jobs
- -s - show only stopped jobs
Background
Next, I'll resume the sleep
job in the background. To do this, I use the bg
command. Now, the bg
command has a pretty simple syntax, as seen here:
bg [JOB_SPEC]
Where JOB_SPEC can be any of the following:
- %n - where n is the job number
- %abc - refers to a job started by a command beginning with abc
- %?abc - refers to a job started by a command containing abc
- %- - specifies the previous job
NOTE: bg
and fg
operate on the current job if no JOB_SPEC is provided.
I can move this job to the background by using the job number [1].
[tcarrigan@rhel ~]$ bg %1
[1]+ sleep 500 &
You can see now that I have a single running job in the background.
[tcarrigan@rhel ~]$ jobs
[1]+ Running sleep 500 &
Foreground
Now, let's look at how to move a background job into the foreground. To do this, I use the fg
command. The command syntax is the same for the foreground command as with the background command.
fg [JOB_SPEC]
Refer to the above bullets for details on JOB_SPEC.
I have started a new sleep
in the background:
[tcarrigan@rhel ~]$ sleep 500 &
[2] 5599
Now, I'll move it to the foreground by using the following command:
[tcarrigan@rhel ~]$ fg %2
sleep 500
The fg
command has now brought my system back into a sleep state.
The end
While I realize that the jobs presented here were trivial, these concepts can be applied to more than just the sleep
command. If you run into a situation that requires it, you now have the knowledge to move running or stopped jobs from the foreground to background and back again.
[ Free online course: Red Hat Enterprise Linux technical overview. ]
About the author
Tyler is the Sr. Community Manager at Enable Sysadmin, a submarine veteran, and an all-round tech enthusiast! He was first introduced to Red Hat in 2012 by way of a Red Hat Enterprise Linux-based combat system inside the USS Georgia Missile Control Center. Now that he has surfaced, he lives with his wife and son near Raleigh, where he worked as a data storage engineer before finding his way to the Red Hat team. He has written numerous technical documents, from military procedures to knowledgebase articles and even some training curricula. In his free time, he blends a passion for hiking, climbing, and bushcraft with video games and computer building. He is loves to read and enjoy a scotch or bourbon. Find him on Twitter or on LinkedIn.
Browse by channel
Automation
The latest on IT automation for tech, teams, and environments
Artificial intelligence
Updates on the platforms that free customers to run AI workloads anywhere
Open hybrid cloud
Explore how we build a more flexible future with hybrid cloud
Security
The latest on how we reduce risks across environments and technologies
Edge computing
Updates on the platforms that simplify operations at the edge
Infrastructure
The latest on the world’s leading enterprise Linux platform
Applications
Inside our solutions to the toughest application challenges
Original shows
Entertaining stories from the makers and leaders in enterprise tech