Subject: Re: Daemon
To: Yosep <yoskim@NMSU.Edu>
From: Frederick Bruckman <fredb@immanent.net>
List: netbsd-users
Date: 07/02/2003 13:16:40
On Wed, 2 Jul 2003, Yosep wrote:

> I've just created what I think is a daemon, however, I am not sure how it
> actually works.  Here is what I have done.

[*]

> I put an executable shell script in my /usr/pkg/sbin. I also made a
> myfiled and put it in /usr/pkg/etc/rc.d directory.  Since, I would like to
> run the daemon using a PID file, I captured a PID of a session running in
> background and put it in /var/run/ directory.  Thus, myfiled looks like
> the following
>
> #
> # $NetBSD$
> #
>
> # PROVIDE: myfiled
> # REQUIRE: DAEMON
> # BEFORE:  LOGIN
>
> .. /etc/rc.subr
>
> name="myfiled"
> rcvar=${name}
> command="/usr/pkg/sbin/${name}"
> pidfile="/var/run/myfile.pid"
>
> load_rc_config $name
> run_rc_command "$1"
>
>
> Am I doing this right?  And if I am doing it right, how can I put this to
> work?  I am not quite sure how to start a daemon or stop it.  I tried
> something like /usr/pkg/etc/rc.d beacond start ... but didnt work.

First of all, why put it in "/usr/pkg" if it's not a package? The more
normal thing, IMO, would be to put the executable in
"/usr/local/sbin", and the rc.d file in "/etc/rc.d". Now is it called
"myfiled" or "beacond"? Anyway, let's assume it's called "myfiled" as
in your example rc.d file. You'd like to do, simply

    /etc/rc.d/myfiled start
    /etc/rc.d/myfiled stop
    /etc/rc.d/myfiled restart
    /etc/rc.d/myfiled status

and so on, but that won't do anything unless you already have either

    myfiled=YES

in "/etc/mk.conf", or if you've exported "myfiled=YES" into your
environment. If you just want to start it once, for testing, you
can use

    /etc/rc.d/myfiled forcestart
    /etc/rc.d/myfiled forcestop

> Any help would be greatly appreicated, and also any suggestions to online
> documentations on dealing with daemon would be great!
> thanks a lot.

That's all there is to the NetBSD start-up scripts. If you're writing
a daemon, in say, C, there's a lot more you should know. Maybe start
with the comp.unix.programming FAQ:

    http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16


[*] Assuming that you're running on NetBSD, "fstat | grep myfiled"
would have told you right away that your daemon wasn't running.


Frederick