in reply to how to make a demon in perl?

There is one important point that wasn't raised by the other posts, or by the FAQ entry linked above.

Your program should "chdir '/'" or the equivalent if at all possible, so you don't keep a working directory on a mounted device.

If the device has to be unmounted for maintenance, it would be "busy" and locked if your process still has its current working directory there.

That's not usually a problem for non-daemon processes, since the sysadmin can boot off all interactive users without taking the system down. But if you're "half-daemonized", they'll either have to track down which process has the directory in use, or reboot the box.

If you're going to be logging to a file, chdir to that directory; that way, only one filesystem is locked down.
--
Mike

Replies are listed 'Best First'.
Re: Re: how to make a demon in perl?
by Sihal (Pilgrim) on Oct 01, 2002 at 12:52 UTC
    what is the difference between chdir to the "/" directory or to /var/log/ for ex? which one is the safest ? I had a look at the daemonize module; looks pretty neat.
      Type "mount" at your command line. You'll see a listing something like this:
      $ mount /dev/sda1 on / type ext3 (rw) none on /proc type proc (rw) /dev/sda7 on /data type ext3 (rw) none on /dev/pts type devpts (rw,gid=5,mode=620) none on /dev/shm type tmpfs (rw) /dev/sda5 on /tmp type ext3 (rw) /dev/sda3 on /usr type ext3 (rw) /dev/sda2 on /var type ext3 (rw) nfsserver:/vol/office/home on /home type nfs (rw,addr=nfsserver) nfsserver:/vol/office/log on /log type nfs (rw,addr=nfsserver)
      Each of the /dev/sd\d items is a different disk on your system, and the "type nfs" entries are remote volumes you're connected to.

      The system can't operate without / being mounted, so it doesn't make a difference if that's your current working directory. If / is dead, the system definitely needs a reboot anyways, at least.

      /var/log (well, /var at least) is usually a separate device, which the system could theoretically live without.

      In fact, if your daemon logs via syslog, as such things probably should, the admin can unmount /var/log even if lots of daemons normally log there by reconfiguring syslogd to log somewhere else.

      But, in general, /var/xxx is probably an ok 3rd choice, with / being best, and /tmp probably being 2nd.
      --
      Mike

        BTW I know about how unix works :-) thanx for the advice anyway.
      If you have a log file you're writing to in /var/log, then chdir to /var/log - you're already going to keep /var/log busy, might as well not keep / busy too. If you're not logging directly, (perhpas you're using syslog, like a good daemon), then chdir to /.

      -- Dan