Master the Linux ‘jobs’ Command: A Comprehensive Guide

Peter Hou
3 min readJun 7, 2023

--

This guide provides a comprehensive understanding of the Linux ‘jobs’ command. It delves into its history, its uses, and common parameters, giving beginners the necessary skills to use this command effectively. Understanding ‘jobs’ is essential for efficient multitasking and process management in Linux.

Instructions

This guide provides a thorough overview of the Linux ‘jobs’ command, designed for beginners. We will delve into its history, usage, common parameters, and practical use cases. We’ll also explore some unusual usage techniques and important considerations.

History

The ‘jobs’ command has been a part of Unix and Unix-like operating systems since the creation of the Bourne shell in the 1970s. It was designed as a built-in shell command to manage jobs in the shell.

When and why to use it

The ‘jobs’ command is used when managing processes that have been started in the background or have been stopped (also known as “jobs”). It’s essential for multitasking within a single terminal window and managing multiple processes simultaneously.

How to use it

To use the ‘jobs’ command, simply type ‘jobs’ in your terminal. By default, it will list all currently running background jobs.

$ jobs
[1]- Running sleep 30 &
[2]+ Running sleep 45 &

The commonly used parameters

  • -l The '-l' option provides more detail, including the process ID (PID).
$ jobs -l
[1]- 5713 Running sleep 30 &
[2]+ 5714 Running sleep 45 &
  • -p The '-p' option only prints the process ID (PID).
$ jobs -p
5713
5714

Other supported parameters

  • -n Only print a line about stopped or exited jobs if they have changed.
  • -r Only print running jobs.
  • -s Only print stopped jobs.

Most common use cases

The ‘jobs’ command is most often used in conjunction with other job control commands like ‘fg’, ‘bg’, and ‘kill’. These commands allow you to bring jobs to the foreground, continue them in the background, or terminate them entirely.

$ jobs
[1]+ Running sleep 45 &
$ fg 1
sleep 45
^Z
[1]+ Stopped sleep 45
$ bg 1
[1]+ sleep 45 &
$ jobs
[1]+ Running sleep 45 &
$ kill %1
[1]+ Terminated sleep 45
$ jobs

The tricky skills

The ‘jobs’ command can be used creatively in scripts to manage multiple background processes. For example, you can use ‘jobs -p’ to get the PIDs of all background jobs and then use those PIDs for other tasks.

$ sleep 30 &
$ sleep 45 &
$ for pid in $(jobs -p); do echo "PID: $pid"; done
PID: 5713
PID: 5714

What needs to be noted

  • The ‘jobs’ command is a shell built-in, so its behavior might vary between different shells.
  • ‘jobs’ only knows about jobs that were created in the current shell session.

Conclusion

The ‘jobs’ command is a powerful tool for job control in Linux. It allows users to manage multiple processes within a single terminal, providing the ability to multitask efficiently.

--

--

Peter Hou
Peter Hou

Written by Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.

Responses (1)