3

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?

Sylvain Pineau
  • 61,564
  • 18
  • 149
  • 183
Registered User
  • 9,521
  • 14
  • 53
  • 85

1 Answers1

2

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:

NAME
       system - execute a shell command

SYNOPSIS
       #include <stdlib.h>

       int system(const char *command);

DESCRIPTION
       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.
Sylvain Pineau
  • 61,564
  • 18
  • 149
  • 183