codeaprendiz.github.io

DevOps Essentials

View on GitHub

jobs

JOB CONTROL

Job control is nothing but the ability to stop/suspend the execution of processes (command) and continue/resume their execution as per your requirements. This is done using your operating system and shell such as bash/ksh or POSIX shell. Your shell keeps a table of currently executing jobs and can be displayed with jobs command.

DESCRIPTION

It’s a shell builtin Shell builtin commands are commands that can be executed within the running shell’s process.

PURPOSE

Displays status of jobs in the current shell session.

SYNTAX

jobs [-lnprs] [ jobspec … ]

jobs -x command [ args …

The basic syntax is as follows:

OPTIONS

EXAMPLES

Before you start using jobs command, you need to start couple of jobs on your system. Type the following commands to start jobs:

xeyes &
gnome-calculator &
gedit fetch-stock-prices.py &

Finally, run ping command in foreground:

ping www.cyberciti.biz

To suspend ping command job hit the Ctrl-Z key sequence.

To display the status of jobs in the current shell, enter:

$ jobs
[1]   7895 Running                 gpass &
[2]   7906 Running                 gnome-calculator &
[3]-  7910 Running                 gedit fetch-stock-prices.py &
[4]+  7946 Stopped                 ping cyberciti.biz

To display the process ID or jobs for the job whose name begins with “p,” enter:

$ jobs -p %p
[4]-  Stopped                 ping cyberciti.biz

To show process IDs in addition to the normal information

$ jobs -l
[1]  + 47274 suspended  ping google.com

To list only processes that have changed status since the last notification

$ sleep 100 &
$ jobs -n
[5]-  Running                 sleep 100 &

Display lists process IDs (PIDs) only

$ jobs -p
7895
7906
7910
7946
7949

A note about /usr/bin/jobs and shell builtin

$ type -a jobs
jobs is a shell builtin
jobs is /usr/bin/jobs

In almost all cases you need to use the jobs command that is implemented as a BASH/KSH/POSIX shell built-in. The /usr/bin/jobs command can not be used in the current shell. The /usr/bin/jobs command operates in a different environment and does not share the parent bash/ksh’s shells understanding of jobs.