Briareos agreed with me
"
though in my experience, the usual method used by linux daemons is to write a pid file and do operations on the value in that file"
as for why it is so... hrm. I don't actually know, but if I had to guess, then at some point in the early days of *n*x, process table examination was one or more of: difficult, slow, error prone, misleading, insecure, obscure, OS-dependent, and/or language-dependent. rather than fix whatever issue or issues there were, they made a "
standard"
of putting daemon pids in /var/run. just a guess. it's probably an interesting historical question about the evolution of *n*x OS.
another interesting question: do you write "
Operating/System"
or "
Operating System"
. if the latter, why do you abbreviate it with a slash?
anyway: I can think of 7 things which might be related to the reason, or might be the reason, or might just be side notes:
1) a program can (or used to be able to) blatantly lie on the process table by injecting any string it desires into argv[0]. say I maliciously want to prevent sendmail from starting as a daemon. if know that it checks the process table for "
sendmail -d"
(or whatever), I could start up a program which makes the same name appear (and then sleep(5000)s or something) without having to have secured access to /var/run/sendmail.pid
2) say instead of having a separate bash script as the nanny, like you did, I wrote self-nannying behavior into my code: i.e. on startup I look for a program named the same thing as me with my startup options already running... then I don't start if I find a pid file and find that pid exists. this is probably "
normal"
for C daemons, in fact - that way you can just cron "
startItUp -d"
every 5 minutes and rely on it never starting itself more than once... except if I'm examining the process table for "
startItUp -d"
, I myself am already listed there!
3) a daemon like apache forks copies of itself, but only the parent pid is kept in /var/run
4) "
perfectly normal operations"
for a daemon
might include having a root copy which writes a pid to /var/run, and user copies which write pids /home/$USERNAME/var/run or somesuch.
5) a daemon might not be a single program at all, but a chain of programs - grepping ps (or writing equivalent direct API calls) might be difficult under such circumstances since there might be half a dozen strings to look for instead of one.
6) "
kill -1 `cat /var/run/sendmail.pid` "
is easier to type than "
kill -1 `some long ps/grep command to find the pid of sendmail` "
7) it might be "
normal"
for my daemon to "
wrap up the part that matters"
and remove the .pid file long before it's done running - like if it mails the results of the last operation out.
all these ideas have flaws and workarounds, of course (especially with some more tools, like a kill that takes process names instead of PIDs as arguments), so this might just be useless information.
____________________________
[Last edited by silver at 10-15-2007 10:53 AM]