9

jobs is my favorite command to see my codes which are running in the background. In order to check for them dynamically, I tend to type

watch 'jobs'

which does not display anything. However

watch 'ps'

works perfectly. I have been doing the same mistake for months now.

I think understanding why the first does not work while the second works can help me to stop doing the same error.

Can anyone help?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Many
  • 257
  • 1
  • 6
  • 2
    https://superuser.com/questions/460150/how-do-i-use-the-watch-and-jobs-commands-together-in-bash – Thomas Jul 27 '19 at 12:32
  • 1
    A more useful question would be “**how** can I get it to work?” However why (in this case) is more interesting. – ctrl-alt-delor Jul 27 '19 at 17:11

1 Answers1

20

jobs is a built-in that reports on the state of the current shell: the commands that were backgrounded with that shell. watch runs a new shell for each execution, and that shell's jobs has no way of knowing what watch's parent shell's jobs are. ps is an external command and it never used the shell's state.

muru
  • 69,900
  • 13
  • 192
  • 292