title: Supervisord Monitoring of grandchild process
body: <p>We have a service <strong>xyz</strong> that gets started by a script <strong>run-xyz</strong> [This script has a bunch of pre-activation steps before starting up the service <strong>xyz</strong>]</p>
  <p>My goal is to monitor the process <strong>xyz</strong> and for that reason we are using supervisord with the following configuration:</p>
  <pre><code>[program:xyz]
  command         = /path/to/run-xyz
  numprocs        = 1
  priority        = 100
  startsecs       = 10
  startretries    = 10
  stopwaitsecs    = 60
  exitcodes       = 0
  autostart       = false
  autorestart     = true
  redirect_stderr = true
  stdout_logfile  = /path/to/log
  </code></pre>
  <p>The script <strong>run-xyz</strong> starts up the service <strong>xyz</strong> and has an infinite loop checking whether the process <strong>xyz</strong> is present or not. [For example, on a <strong>SIGKILL</strong> of the process id of <strong>xyz</strong>, it comes out of the infinite loop and the script stops running and supervisord automatically restarts <code>[program:xyz]</code> after which things get back to normal]</p>
  <p>However, if the <strong>run-xyz</strong> process gets killed somehow, then the <strong>xyz</strong> service becomes an orphan with parent process id as <strong>1</strong>. Supervisord restarts <code>[program:xyz]</code> but then <code>xyz</code> should not be killed and restarted as <strong>xyz</strong> is already running and script <strong>run-xyz</strong> exits, which eventually leads to <code>start-retries</code> getting over and <code>[program:xyz]</code> reaches a <strong>FATAL</strong> state and now supervisord is not monitoring <code>[program:xyz]</code> anymore.</p>
  <p>I have the following questions:</p>
  <ol>
  <li>Is there a way for <strong>run-xyz</strong> to attach itself as the parent process id of <strong>xyz</strong> in scenarios when it finds <strong>xyz</strong> running with parent process id as <strong>1</strong>?</li>
  <li>Can supervisord monitor the <strong>xyz</strong> pid and do a restart of <code>[program:xyz]</code> when <strong>xyz</strong> pid does not exist anymore. We do not care if <strong>run-xyz</strong> is running or not as it's only job is to get <strong>xyz</strong> started. [Tried the <strong>pidproxy</strong> route but did not work, will explain it below]</li>
  <li>Is supervisord the right choice for what we are trying to achieve?</li>
  </ol>
  <p>Using <strong>pidproxy</strong>, the configuration became:</p>
  <pre><code>[program:xyz]
  command         = /path/to/pidproxy /path/to/xyz.pid /path/to/run-xyz
  numprocs        = 1
  priority        = 100
  startsecs       = 0
  startretries    = 10
  stopwaitsecs    = 60
  exitcodes       = 0
  autostart       = false
  autorestart     = true
  redirect_stderr = true
  stdout_logfile  = /path/to/log
  </code></pre>
  <p>When using the above configuration, we removed the infinite loop in <strong>run-xyz</strong> wherein it starts up <strong>xyz</strong>, sleeps for a few seconds(30) and exits. But supervisor is not waiting and it is assuming that <strong>run-xyz</strong> has exited and again tries to spawn up and this happens until the retries are over it reaches a <strong>FATAL</strong> state. Note: <code>/path/to/xyz.pid</code> exists and the pid gets written in the file.</p>