Dec 13, 2023 5 min read

How to Use jobs command on Linux

Use the jobs command on linux with our step-by-step tutorial. It allows you to view and manage background jobs running within a shell session.

Use jobs command on Linux
How to Use jobs command on Linux
Table of Contents

Introduction

Before we start talking about how to use jobs command on Linux, let's briefly understand-What is a job Command?

The jobs command on Linux allows you to view and manage background jobs running within your current shell session. It provides a convenient way to monitor and control jobs that have been started in the background using the & notation. With the jobs command, you can list running and stopped jobs, bring jobs to the foreground or background, and manage their status.

On Linux, users can interact directly with processes running in the current shell by using the jobs command.

In this tutorial, we will explore how to effectively use the jobs command on Linux.

Job Control performs a variety of tasks. Each may be operated separately. The Job Control commands are listed in the following table.

Command Description
jobs Lists jobs in current shell
bg %n Send process to background
fg %n Bring process to foreground

In the table, n stands for the corresponding job ID's number.

Understanding job control on Linux

In any case, what really is a job? The terms task, procedures, and jobs are sometimes used interchangeably.

Actually, there are a few minor variations that are crucial for clarity.

The difference comes down to which entity is in charge of the tasks. Jobs belong to users, while processes belong to the kernel.

Depending on the Linux operating system, a job may have a single process or numerous childs or subprocesses.

A task with multiple piped commands like these might serve as an illustration of this:

cat file1 | sort | uniq

Using the combination of the sort and uniq commands, it parses the file's output and sorts for unique values.

The operating system on personal computers performs the majority of tasks automatically. This is frequently optimized for the needs of the system or the priority assigned to a specific job. The kernel completes these processes and assigns them a process ID (PID).

How to control jobs on Linux

You have the option to create your own jobs when you open a new shell. In actuality, this is what occurs whenever you type any command (or set of commands) into a terminal.

From a shell at the user level, you can manually send jobs to run in the background, bring them to the foreground, or suspend them (by pressing Ctrl+z).

So let's use a few examples to put everything you have learned so far into practise.

Example 1: Send Jobs to Background

It is simple to send a job to run in the background. You only need to add a & (ampersand) sign to the end of your command.

rohit@vegastack:~$ nano VegaStack &
[1] 2860

You can see that this command returns the job number in brackets. The process ID that the kernel uses is as follows.

Example 2: View Jobs

You could be perplexed if you tried to type the jobs command, since nothing appeared to happen. This is due to the fact that the shell has no running jobs. You can successfully run the jobs command now that a job is running.

rohit@vegastack:~$ jobs
[1]+  Stopped                 nano VegaStack

You don't require the PID number, which is why it isn't listed here. Using the user- or shell-specific job ID, you can manage jobs.

Example 3: Bring Jobs to Foreground

By using that Job ID, you can highlight your nano instance.

rohit@vegastack:~$ fg %1

If you've been following along, clicking this should open Nano and show you your Linux Handbook file.

Example 4: Stop a Job with Ctrl+z

Ctrl+z can be used to end open jobs. The use of this with top is seen in the following screenshot.

You can watch me open top in the screencast, use Ctrl+z to suspend it, view it in my jobs list, bring it back to the foreground, and then close it.

Example 5: Run Background Jobs Using bg

Even though the & sign can be used to send jobs to the background, it's always a good idea to have a backup plan. You saw me use ctrl+z to terminate the job in the screencast. I used the fg%n command to bring the job back to the shell and the foreground. The same thing may be done using bg.

To switch things up, I'm going to use a GUI application. You may have noticed in the past that when a GUI is opened from a terminal, the GUI also closes if the terminal is closed. I hope this tutorial has made it clear why that occurs.

So, let us use Gedit. I'm going to carry out the following:

  • Launch Gedit.
  • Ctrl+z to return to the Shell.
  • Use bg to send it to the background, so it can run.
  • Check my Jobs List to ensure that it is active.
  • Go back to the foreground and send a system interrupt, terminating it with Ctrl+c.
rohit@vegastack:~$ gedit
^Z
[1]+  Stopped                 gedit
rohit@vegastack:~$ bg %1
[1]+ gedit &
rohit@vegastack:~$ jobs
[1]+  Stopped                 gedit
rohit@vegastack:~$ fg %1
gedit
^C

Bonus Tip on termination jobs on Linux

Ctrl+C is a commonly used shortcut to end a job in a shell. Another option is to incorporate your new job control knowledge.

rohit@vegastack:~$ fg %1
gedit
^Z
[1]+  Stopped                 gedit
rohit@vegastack:~$ kill %1

Yes, you may also terminate a job using the kill command and the job id.

FAQs: Using the jobs Command on Linux

What is the purpose of the jobs command?

The jobs command allows you to manage background jobs within your current shell session. It provides information about running and stopped jobs and allows you to control their status or bring them to the foreground or background.

How do I list running jobs using the jobs command?

To list running jobs, simply run the jobs command without any additional options. It will display a numbered list of all running jobs along with their status and job IDs.

Can I see detailed information about a specific job with the jobs command? 

No, the jobs command does not provide detailed information about a specific job. For more detailed information about a job, you can use tools like ps or top.

How do I bring a job to the foreground using the jobs command?

To bring a job to the foreground, you can use the fg command followed by either the job ID or the % character and the job's number as shown in the jobs command output.

Can I send a running job to the background using the jobs command? 

Yes, you can use the bg command followed by either the job ID or the % character and the job's number to send a running job to the background. For example, bg %2 sends job number 2 to the background.

How can I stop or pause a running job using the jobs command? 

To stop or pause a running job, use the Ctrl+Z key combination. This will send a SIGTSTP (terminal stop) signal to the job, suspending its execution

Can I resume a stopped job using the jobs command? 

Yes, you can resume a stopped job using the fg or bg commands. The fg command brings the job to the foreground, while the bg command sends it to the background.

Conclusion

The jobs command on Linux provides a useful way to manage background jobs within your current shell session. By using the jobs command along with its associated options, you can list running and stopped jobs, bring them to the foreground or background, and control their execution status.

Whether you are multitasking on the command line or need to manage long-running processes, the jobs command offers valuable functionality to monitor and control background jobs efficiently.

We have a better understanding of how our preferred operating system operates by learning the Linux command line.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.