How do I use the watch
command with jobs
command, so that I can monitor when a background job is finished?
I execute it as follows, but I do not get the output from jobs:
watch jobs
If I run jobs by itself, I get the following with my test background job:
jobs
[1]+ Running nohup cat /dev/random >/dev/null &
Best Answer
The
watch
command is documented as follows:The part about giving the command to
sh -c
means thejobs
command you are running viawatch
is running in a different shell session than the one that spawned the job, so it cannot be seen that other shell. The problem is fundamentally thatjobs
is a shell built-in and must be run in the shell that spawned the jobs you want to see.The closest you can get is to use a while loop in the shell that spawned the job:
You could define a function in your shell startup script to make that easier to use:
Then you just have to type
myjobwatch
.