Jobs command example
I am a new Linux/Unix user. In Linux or Unix-like systems, how can I use BASH/KSH/TCSH or POSIX-based shell to view the job in progress )? On Unix/Linux, how does one display the current job status? (LCTT: job, also known as "task ")
Job control is a capability that can stop/pause the execution of processes (commands) and continue/resume their execution as required. This is executed through your operating system and shell such as bash/ksh or POSIX shell.
Shell stores the currently executed jobs in a table and can be displayed using the jobs command.
Purpose
Displays the job status in the Current shell session.
Syntax
The basic syntax is as follows:
jobs
Or
jobs jobID
Or
jobs [options] jobID
Start some jobs for demonstration
Before using the jobs command, you must start multiple jobs on the system. Run the following command to start the job:
### Start xeyes, calculator, and gedit text editor ###
xeyes &
gnome-calculator &
gedit fetch-stock-prices.py &
Finally, run the ping command on the foreground:
ping www.cyberciti.biz
Press Ctrl-Z to suspend the (suspend) ping command job.
Jobs command example
To display the job status in the current shell, enter:
$ jobs
Output example:
[1]7895Running gpass &
[2]7906Running gnome-calculator &
[3]-7910Running gedit fetch-stock-prices.py &
[4]+7946Stoppedping cyberciti.biz
To display the process ID or job name whose name starts with "p", enter:
$ jobs -p %p
Or
$ jobs %p
Output example:
[4]-Stoppedping cyberciti.biz
Character % is a method for specifying a task. In this example, you can use a string starting with the job name to pause it, for example, % ping.
How does one display that the process ID does not contain other normal information?
Use the-l (lower-case L) option of the jobs command to list the details of each job. Run:
$ jobs -l
Sample output:
Fig.01: display the jobs status in shell
How to list only the processes with the last state change?
First, start a new job as follows:
$ sleep100&
Now, only jobs that have been stopped or exited since the last time are displayed. Enter:
$ jobs -n
Sample output:
[5]-Runningsleep100&
Show only process ID (PID)
The-p option of the jobs command only displays PID:
$ jobs -p
Sample output:
7895
7906
7910
7946
7949
How can I only display running jobs?
The-r option of the jobs command only displays running jobs. Enter:
$ jobs -r
Sample output:
[1]Running gpass &
[2]Running gnome-calculator &
[3]-Running gedit fetch-stock-prices.py &
How to show only jobs that have stopped working?
Use the-s option of the jobs command to display only the jobs that stop the job. Enter:
$ jobs -s
Sample output:
[4]+Stoppedping cyberciti.biz
To continue to run the ping cyberciti. biz job, enter the following bg command:
$ bg %4
Jobs Command Option
From the bash (1) command man manual page:
Option |
Description |
-l |
Lists process IDs and other information. |
-p |
Only process IDs are listed. |
-n |
Only lists the processes with status changes that have occurred since the last time the Status Change Prompt (for example, a process exits) is displayed. |
-r |
Only running jobs are displayed. |
-s |
Only the stopped jobs are displayed. |
-x |
Run the command and its parameters, and replace the process group ID of the matched original job with the process ID of the new command. |
Description of/usr/bin/jobs and shell built-in
Run the following type command to check whether the jobs command is a shell built-in command or an external command is still:
$ type -a jobs
Output example:
jobs is a shell builtin
jobs is/usr/bin/jobs
In almost all cases, you need to use the built-in jobs command of BASH/KSH/POSIX shell. The/usr/bin/jobs command cannot be used in the current shell. The/usr/bin/jobs command works in different environments and does not share shell jobs of its parent bash/ksh.
Via: http://www.cyberciti.biz/faq/unix-linux-jobs-command-examples-usage-syntax/
Author: Vivek Gite Translator: strugglingyouth Proofreader: wxy
This article was originally compiled by LCTT and launched with the honor of Linux in China
This article permanently updates the link address: