Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 23 Mar 1999 17:35:35 +1030
From:      Greg Lehey <grog@lemis.com>
To:        cjclark@home.com, FreeBSD Questions <freebsd-questions@FreeBSD.ORG>
Subject:   Re: Process Checking
Message-ID:  <19990323173535.J442@lemis.com>
In-Reply-To: <199903230637.BAA10035@cc942873-a.ewndsr1.nj.home.com>; from Crist J. Clark on Tue, Mar 23, 1999 at 01:37:19AM -0500
References:  <199903230637.BAA10035@cc942873-a.ewndsr1.nj.home.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tuesday, 23 March 1999 at  1:37:19 -0500, Crist J. Clark wrote:
> I've got a question about monitoring a daemon. It's a server for a
> game and not the most stable piece of software you have ever seen. I
> want to run a cron job periodically to check if the process has not
> died, and if it has, restart it.

The obvious way to do this is write a little C program which in a loop
spawns the daemon and then wait4()s it to die.  The following code
should do the trick:

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <string.h>
extern int errno;

int main (int argc, char *argv [], char *envp [])
{
  pid_t pid;
  int status;
  struct rusage rusage;

  while (1)
    {
    pid = fork ();
    switch (pid)
      {
    case 0:						    /* child */
      execve (argv [1], &argv [1], envp);
      printf ("Couldn't execve %s: %s\n", argv [1], strerror (errno));
      exit (1);						    /* death */
      /* NOTREACHED */

    case -1:
      perror ("Can't fork");
      exit (1);

    default:
      wait4 (pid, &status, 0, &rusage);
      printf ("Pid %d died\n", (int) pid);
      }
    }
  }

Just supply the name of the daemon and any parameters, and it should
keep the daemon running.

Greg
--
When replying to this message, please copy the original recipients.
For more information, see http://www.lemis.com/questions.html
See complete headers for address, home page and phone numbers
finger grog@lemis.com for PGP public key


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?19990323173535.J442>