Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 &
share|improve this question

2 Answers 2

up vote 1 down vote accepted

The watch command is documented as follows:

SYNOPSIS
   watch  [-dhvt]  [-n  <seconds>] [--differences[=cumulative]] [--help]
          [--interval=<sec-onds>] [--no-title] [--version] <command>
[...]
NOTE
   Note that command is given to "sh -c" which means that you may need to
   use extra quoting to get the desired effect.

The part about giving the command to sh -c means the jobs command you are running via watch 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 that jobs 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:

$ while true; do jobs; sleep 10; done

You could define a function in your shell startup script to make that easier to use:

myjobwatch() { while true; do jobs; sleep 5; done; }

Then you just have to type myjobwatch.

share|improve this answer

you could use a while loop and make an function for this

while true; do echo -ne "`jobs`\r"; sleep 1;  done
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.