From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f678f,82f1e5f281b74d85 X-Google-Attributes: gidf678f,public From: Carl Beckner Subject: Re: daemon construction Date: 1998/06/02 Message-ID: <3573E7B6.E7DE2B3@dinker.pprd.abbott.com>#1/1 X-Deja-AN: 359272289 Content-Transfer-Encoding: 7bit References: <6kuhfh$17d2@fizban.pprd.abbott.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Organization: Abbott Newsgroups: comp.unix.osf.misc Dirksen E. Bussiere [42T] wrote: > > I need to write a daemon which will run in the background on our > alphaserver 4100 and monitor the execution of certain software > tasks. In essence, it will be a fancy queue manager. I've > written such programs before, but they were on smaller systems and > in assembler (utilizing NMI's). I want to write this one in C. > Does anyone know where I can find any source code examples of this > kind of program for the alpha, or of any examples? Thanks. Essentially you need 2 things. First, you need a little script to start your daemon when the system boots. On Digital UNIX, the convention is to put this script in /sbin/init.d, then create a link in /sbin/rc3.d to the script. The script should begin with an upper case "S" to indicate it is used on startup. Here is an example of a script I use. #!/sbin/sh # # Start robot control program # # Start ssqzym # if [ -x /usr/bin/ssqzym ]; then /usr/bin/ssqzym >/dev/null 2>&1 & /bin/echo "Robot control program started" fi Then you need a little bit of code in your daemon to make sure it runs in the background, closes the connection to the controlling terminal, etc. Here is the routine I use for that. void daemon_init (void) { pid_t pid; char buff[80]; if ((pid = fork ()) < 0) { errlog ( "Error forking ourself as a daemon process - exiting" ); exit ( 1 ); } else if (pid != 0) { sprintf ( buff, "Parent has forked child %d - exiting\n", pid ); errlog ( buff ); exit ( 0 ); } /* * Set the process & session ID * also releases the controlling terminal */ setsid(); setpgrp(); /* * Change to a known valid directory */ chdir ("/"); umask (0); } Hope this helps. Carl -- Carl Beckner becknerc@dinker.pprd.abbott.com Abbott Laboratories Phone: 847-938-2217 Dept 42T; Bldg AP31 Fax : 847-938-9226 200 Abbott Park Rd Abbott Park, Il 60064