title: How to repeatedly restart a program via signal
body: <p>I wanted a convenient way to restart a program. I thought I could just catch a signal (USR1 in the example) and call exec.</p>
  
  <pre><code>#include &lt;signal.h&gt;
  #include &lt;unistd.h&gt;
  #include &lt;stdio.h&gt;
  
  char* const* args;
  void restart() {
      printf("restarting\n");
      execv(args[0], args);
  }
  
  int main(int argc, char* const argv[]) {
      printf("Starting\n");
      args = argv;
      signal(SIGUSR1, restart);
      raise(SIGUSR1); // could use pkill -SIGUSR1 file-name instead
      pause();
      printf("Terminated normally\n");
      return 0;
  }
  </code></pre>
  
  <p>The above example kinda works. The output is</p>
  
  <pre><code>Starting
  restarting
  Starting
  </code></pre>
  
  <p>and then it hangs. Other signals can still be received. </p>
  
  <p>I assume I'm just failing to clear the signal. The expected behavior is for program to keep restarting indefinitely.</p>