- How to run a job in background?
- How to list background jobs?
- How to send a job running in background to foreground?
- How to send an already running foreground job to background?
How to run a job in background?
To run a job / command in background put an ampersand mark- & at the end of the job name or command separated by a space as shown below:
-bash-2.05b# top &
[1] 1514
-bash-2.05b# vi web.log &
[2] 1515
[1]+ Stopped top
-bash-2.05b#
How to list background jobs?
To view the jobs we have run in background, type jobs as shown below:
-bash-2.05b# jobs
[1]- Stopped top
[2]+ Stopped vim web.log
-bash-2.05b#
When we run a job in background with & its PID is shown along with the job number as shown above. However, the PID is not shown in the output generated by the command jobs. We can use -l with jobs to display the PID and the status of the job as shown below:
-bash-2.05b# jobs -l
[1]- 1514 Stopped (tty output) top
[2]+ 1515 Stopped (tty output) vim web.log
-bash-2.05b#
How to send a job running in background to foreground?
To send the job in foreground we can use its job number as shown below:
-bash-2.05b# fg 1
The above command would send the job "top" in the foreground and it would become the currently active job.
How to send an already running foreground job to background?
Let's first execute a job as usual in foreground:
-bash-2.05b# watch -d uptime
The above command would run in real-time. To get back to the Shell Prompt we can hit Ctrl and z simultaneously that stops the job as shown below:
[1]+ Stopped watch -d uptime
-bash-2.05b# jobs -l
[1]+ 1557 Stopped watch -d uptime
-bash-2.05b#
We can then resume the job in background by issuing the following command:
-bash-2.05b# bg 1
[1]+ watch -d uptime &
-bash-2.05b#
Notice the ampersand symbol "&" above it is same as:
-bash-2.05b# watch -d uptime &
[2] 1560
-bash-2.05b# jobs -l
[1]- 1557 Stopped (tty output) watch -d uptime
[2]+ 1560 Stopped (tty output) watch -d uptime
-bash-2.05b#
Note: Hitting Ctrl + c cancels the job. Use Ctrl + z to stop the job as shown above.
No comments:
Post a Comment