How to use the command 'jobs' (with examples)
The ‘jobs’ command is used to display the status of jobs in the current session. It provides information about the running, stopped and terminated jobs. This command is particularly useful when working in a terminal environment, where multiple jobs are running simultaneously.
Use case 1: Show status of all jobs
Code:
jobs
Motivation: In a session with multiple jobs running, it can be useful to quickly check the status of all jobs. This allows the user to see which jobs are running, stopped or terminated, and get a general overview of the current session.
Explanation: This use case of the ‘jobs’ command does not require any argument. It simply displays the status of all jobs in the current session.
Example output:
[1] Running command1
[2]- Stopped command2
[3]+ Done command3
In this example output, the first job is currently running, the second job is stopped and the third job has completed.
Use case 2: Show status of a particular job
Code:
jobs %job_id
Motivation: When a session has multiple jobs running, it can be helpful to check the status of a specific job. This allows the user to gather information about a specific job without scrolling through the entire list of jobs in the session.
Explanation: In this use case, the job_id is the ID of the particular job for which the status needs to be displayed. The job_id is obtained from the output of the ‘jobs’ command.
Example output:
[2]- Stopped command2
In this example output, the status of job with ID 2 is shown, indicating that it is currently stopped.
Use case 3: Show status and process IDs of all jobs
Code:
jobs -l
Motivation: When managing multiple jobs, it can be necessary to have more detailed information about them, including the process IDs (PIDs). This can be useful for further investigation, troubleshooting, or interacting with the jobs using other commands.
Explanation: This use case of the ‘jobs’ command includes the ‘-l’ option, which provides the status and process IDs of all jobs in the current session.
Example output:
[1] 1234 Running command1
[2]- 5678 Stopped command2
[3]+ 9101 Done command3
In this example output, the first job is running with a process ID of 1234, the second job is stopped with a process ID of 5678, and the third job has completed with a process ID of 9101.
Use case 4: Show process IDs of all jobs
Code:
jobs -p
Motivation: Sometimes, it is only necessary to obtain the process IDs of the jobs running in the session, without any other status information. This can be useful when needing to use the process IDs in other commands or processes.
Explanation: This use case of the ‘jobs’ command includes the ‘-p’ option, which displays only the process IDs of all jobs in the current session.
Example output:
1234
5678
9101
In this example output, the process IDs of the jobs running in the session are displayed.