When I tried to run watch ps command I got the following result:

Every 2.0s: ps                                          Thu Jul 31 14:06:45 2014

  PID TTY          TIME CMD
 4329 pts/1    00:00:00 bash
 4380 pts/1    00:00:00 watch
 4381 pts/1    00:00:00 watch
 4382 pts/1    00:00:00 sh
 4383 pts/1    00:00:00 ps

Why does this command starts two watch processes instead of one?

Also why does watch starts an instance of sh?


To find the answer we need to look at the watch command source code.

  1. You can see two watch processes because watch forks (line 380) a new process each time it has to run the command:

    enter image description here

  2. The extra sh process is here because watch calls the system() function (line 399) and by default it runs the specified command with /bin/sh -c. Look at the system man page:

       system - execute a shell command

       #include <stdlib.h>

       int system(const char *command);

       system()  executes a command specified in command by calling /bin/sh -c
       command, and returns after the command has been completed.  During exe‐
       cution  of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT
       will be ignored.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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