From owner-freebsd-questions Mon Mar 22 23: 6: 0 1999 Delivered-To: freebsd-questions@freebsd.org Received: from allegro.lemis.com (allegro.lemis.com [192.109.197.134]) by hub.freebsd.org (Postfix) with ESMTP id 4530815231 for ; Mon, 22 Mar 1999 23:05:56 -0800 (PST) (envelope-from grog@freebie.lemis.com) Received: from freebie.lemis.com (freebie.lemis.com [192.109.197.137]) by allegro.lemis.com (8.9.1/8.9.0) with ESMTP id RAA20763; Tue, 23 Mar 1999 17:35:36 +1030 (CST) Received: (from grog@localhost) by freebie.lemis.com (8.9.3/8.9.0) id RAA93207; Tue, 23 Mar 1999 17:35:35 +1030 (CST) Message-ID: <19990323173535.J442@lemis.com> Date: Tue, 23 Mar 1999 17:35:35 +1030 From: Greg Lehey To: cjclark@home.com, FreeBSD Questions Subject: Re: Process Checking References: <199903230637.BAA10035@cc942873-a.ewndsr1.nj.home.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 0.93.2i 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 WWW-Home-Page: http://www.lemis.com/~grog Organization: LEMIS, PO Box 460, Echunga SA 5153, Australia Phone: +61-8-8388-8286 Fax: +61-8-8388-8725 Mobile: +61-41-739-7062 Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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 #include #include #include #include #include #include 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