In the Linux command line, we often need to run programs in the background. The jobs
command is a useful tool to list the status of the jobs that we have started.
When we run a command in Linux or macOS, we can append the &
symbol after the command to execute it in the background. This is particularly helpful for long-running programs. For example, to run the top
command in the background, we can use the following command:
top &
To switch back to a program running in the background, we can use the fg
command. If we have multiple jobs in the background, we need to specify the job number with fg
. For instance, fg 1
will bring the first job to the foreground, fg 2
will bring the second job, and so on.
To obtain the job number, we can use the jobs
command. Let’s say we have executed top &
and top -o mem &
, resulting in two instances of the top
program running. By running the jobs
command, we can see the job status as shown below:
[1]- Running top &
[2]+ Running top -o mem &
Now, we can switch back to one of these jobs using the fg <jobid>
command. Pressing cmd-Z
will stop the program again.
Additionally, running jobs -l
will not only display the job status but also provide the process ID of each job.
Please note that the jobs
command is compatible with Linux, macOS, WSL, and any other UNIX environments.
Linux, macOS, command line, job control