[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: how to start an obnoxious daemon at boot?



Quoting Ethan Benson (erbenson@alaska.net):
> 
> I have a BestPower UPS (it is a `smart' one) and I am trying to start 
> the monitoring daemon with a /etc/init.d/ script like everything else 
> (using the init.d/skeleton for a template), however i have a problem:
> 
> this daemon does not create .pid files, and it forks several times 
> when its first starting up so start-stop-daemon --make-pidfile  gets 
> the pid of a process that is killed as soon as the daemon is finished 
> connecting to the UPS, so the `stop' part of the script won't work :(

Hmm. This is new to potato. I don't know how it can work with any
normal daemon as they should always fork twice before writing their
(by now, new) pid into the pidfile.

> and if that is not enough this daemon always leaves a zombie process 
> after it starts up, so trying to fix the .pid with pidof does not 
> work either since it finds 2 pids...

Hmm. Maybe the zombie is there just as a tenuous link to the daemon.
Where is this sort of behaviour documented, I wonder.

> I have been trying various scripting tricks to replace 
> start-stop-daemon creating an empty file in /var/run so i can tell 
> when its running or not, the other problem I have is the way this 
> daemon forks and exits it seems to cause my script to aport 
> prematurely.

Some possibilities:
Children will have higher PIDs, but allow for rollover at 32768.
So the child that does the work will have the highest number.

Does fuser help? The child may open some files to do its work.
The parent will probably only have the original 0, 1, 2 which
the child should have closed as part of its daemonification.

You say the script exits. The line that starts the program should
return as soon as the parent exits. Unless your script has a loop
or a wait on the right pid, it should exit. Or do you mean it
aborts on some sort of error? 

> BestPower supplies the full source code to their software (and gives 
> full specification on the comm protocol to anyone who asks), but I am 
> not skilled enough of a programmer to fix these problems (that and 
> the code is kinda gross...)

What's it written in?

> does anyone have any suggestions for how to do this?

The code to daemonify is pretty standard, consisting as it does of
little more than:

umask
create and lock the pid file
fork (parent exits)
setsid
fork (parent exits)
chdir
umask
close 0,1,2
write pid file
set SIGCHLD if forking children

I could send you it in python, which is a pretty good pseudocode
for any language.

> does there 
> happen to be a replacement software for these UPSs (that works in 
> smart mode) ?

I think others may have answered this.

Cheers,

-- 
Email:  d.wright@open.ac.uk   Tel: +44 1908 653 739  Fax: +44 1908 655 151
Snail:  David Wright, Earth Science Dept., Milton Keynes, England, MK7 6AA
Disclaimer:   These addresses are only for reaching me, and do not signify
official stationery. Views expressed here are either my own or plagiarised.


Reply to: