Jobs command example

Source: Internet
Author: User

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:

  1. jobs

Or

  1. jobs jobID

Or

  1. 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:

  1. ### Start xeyes, calculator, and gedit text editor ###
  2. xeyes &
  3. gnome-calculator &
  4. gedit fetch-stock-prices.py &

Finally, run the ping command on the foreground:

  1. 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:

  1. $ jobs

Output example:

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

To display the process ID or job name whose name starts with "p", enter:

  1. $ jobs -p %p

Or

  1. $ jobs %p

Output example:

  1. [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:

  1. $ 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:

  1. $ sleep100&

Now, only jobs that have been stopped or exited since the last time are displayed. Enter:

  1. $ jobs -n

Sample output:

  1. [5]-Runningsleep100&

 

Show only process ID (PID)

The-p option of the jobs command only displays PID:

  1. $ jobs -p

Sample output:

  1. 7895
  2. 7906
  3. 7910
  4. 7946
  5. 7949

 

How can I only display running jobs?

The-r option of the jobs command only displays running jobs. Enter:

  1. $ jobs -r

Sample output:

  1. [1]Running gpass &
  2. [2]Running gnome-calculator &
  3. [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:

  1. $ jobs -s

Sample output:

  1. [4]+Stoppedping cyberciti.biz

To continue to run the ping cyberciti. biz job, enter the following bg command:

  1. $ 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:

  1. $ type -a jobs

Output example:

  1. jobs is a shell builtin
  2. 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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.