How do I use the watch and jobs commands together in Bash?

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.


Rather than a loop, I tend to use watch with a grep for my running job, eg

watch 'ps u | grep rsync'

Not perfect, but better than a while loop :)

Tags:

Linux

Bash

Watch