Subject: Re: [Dev] HOWTO write a daemon

From: "John F Davis" <[email-masked]>
Date: Fri, 24 May 2002 08:43:06 -0400

Hello Mike,

That was a good read.   I like the bit about 'and this unknown led to this
unknown etc'.  You tracked down each unknown in turn and learned a lot.  I
can identify with that.

If the original poster wants to talk about what they plan to do in more
detail, I'm sure we can all help.

Thanks

JD

"M. Mueller" <[email-masked]>@trilug.org on 05/23/2002 11:46:14 PM

Please respond to [email-masked]

Sent by:    [email-masked]

To:    [email-masked]
cc:
Subject:    Re: [Dev] HOWTO write a daemon

On Thursday 23 May 2002 09:56 pm, Paul D. Boyle reputedly wrote:

> Under Unix (Linux) all processes automatically open three I/O streams,
> stdin, stdout, stderr, these are often closed in a daemon process.

So, you need alternatives to these services.

I make great use of the syslogger daemon.  I had to become aware of the
klogger, or kernel logger daemon because it would generate useful
information
for me.  This led to needing an awareness of the logrotate function which
led
to needing an awareness of cron from a system point of view.  I "tail -f"
the
/var/log/messages file for long periods of time and found that logrotate
changes the file handles so that the "tail -f" goes dead so to speak.  I've
hacked a script that lets me quickly manage the tail function (script
attached). I eventually learned how to manage the /etc/syslog.conf and
syslog
calls to define new log files associated with new facilities.
Another capability that I think Brent Verner brought to my attention, and
that I have not investigated yet, is the FIFO abilities of the Linux
syslogger.  More at "man syslogd".

My daemons use AF_INET sockets for interprocess communication.  I tried
several flavors of IPC (SystemV, AF_UNIX sockets).  And there are more IPC
methods I did not try.  I've seen code that used disk files for IPC.  The
Stevens book, "UNIX Network Programming, Volume 2, Second Edition:
Interprocess Communications, Prentice Hall, 1999" was my primary resource.
I
use Python to write shell to daemon "communicators".

My intent here is to dump a bunch of ideas to the list that you can pick
through.  If there is further interest, I can provide code detail and
system
configuration detail as requested.

--
Mike Mueller
www.ss7box.com

---

From: "Peter Long" <[email-masked]>
Date: Fri, 24 May 2002 09:24:13 -0400 (EDT)

> Hello Mike,
> 
> That was a good read.   I like the bit about 'and this unknown led to
> this unknown etc'.  You tracked down each unknown in turn and learned a
> lot.  I can identify with that.
> 

Well thanks for the offer to help. I planned to turn my application in to a 
daemon all along so I wrote it to have no interface. It does not rely on 
the existence of stdin/stdout/stderr and I log everything using calls to 
syslog(). So what I needed to know what how to detach the io streams and 
whatever else was necessary for a process to be considered a daemon.

BTW: I was under the impression that just typing 'mydaemon&' would not work.
I thought that the process would be terminated when I logged off in any 
case. Is there some way around that or am I just completely mistaken?

Thanks again for all the help. 

-- 
Peter Long

---

From: Jeremy P <[email-masked]>
Date: Fri, 24 May 2002 09:35:24 -0400 (EDT)

> case. Is there some way around that or am I just completely mistaken?

It won't die if you trap the appropriate signals (like SIGHUP, signal
1).  SIGHUP is normally used to signal child processes that the user
logged off; for daemons it's trapped and is often used to reread the
configuartion file.

Even if you're not writing a program, you can block SIGHUP with the
"nohup" utility... read the man page for nohup (or "pinfo nohup" for the
better documentation).  I think nohup also "nice's" the background
process, which you may or may not want.

--Jeremy

---

From: "Peter Long" <[email-masked]>
Date: Fri, 24 May 2002 12:49:06 -0400 (EDT)

>> BTW: I was under the impression that just typing 'mydaemon&' would not
>> work. I thought that the process would be terminated when I logged off
>> in any  case. Is there some way around that or am I just completely
>> mistaken?
> 
> "nohup" utility... read the man page for nohup (or "pinfo nohup" for
> the better documentation).  I think nohup also "nice's" the background

I assume trapping SIGHUP will work on any form on unix machine. The 
application that I am writing has to work on Win2K, AIX and Linux. Making a 
win2k service I can do. Hopefully daemons are implemented the same on both 
AIX and Linux. :)

-- 
Peter Long
mailto:[email-masked]

---

From: Jeremy P <[email-masked]>
Date: Fri, 24 May 2002 12:48:10 -0400 (EDT)

On Fri, 24 May 2002, [iso-8859-1] Peter Long wrote:

> application that I am writing has to work on Win2K, AIX and Linux.
> Making a win2k service I can do. Hopefully daemons are implemented the
> same on both AIX and Linux. :)

AFAIK, that will work on any Unix-like machine.  I don't have personal
experience with AIX, but it certainly works that way on Solaris as well as
Linux.  Try it out, see what happens!  :-)

--Jeremy