From: Carl Beckner <becknerc@dinker.pprd.abbott.com>
Subject: Re: daemon construction
Date: 1998/06/02 [thread overview]
Message-ID: <3573E7B6.E7DE2B3@dinker.pprd.abbott.com> (raw)
In-Reply-To: 6kuhfh$17d2@fizban.pprd.abbott.com
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
prev parent reply other threads:[~1998-06-02 4:00 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
1998-06-01 4:00 daemon construction Dirksen E. Bussiere [42T]
1998-06-02 4:00 ` Carl Beckner [this message]
read-only archive
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox