How to daemonize a program ?

Hello everybody, and first of all, sorry for my bad english.

I've run Linux systems for a while (Debian, Fedora...) and for the first time, I have to wrok on FreeBSD.
I've got a program cho looks like that :

Code:
while true
do
for file in `find ./ -name \*atraiter\*
do
<some things here>
done
sleep 10
done

On Ubuntu system, I've made a script in /etc/init.d using start-stop-daemon, and it launches on system startup.
I must do the same on FreeBsd. I've tried that :
Code:
# PROVIDE: demon

. /etc/rc.subr

name="demon"
rcvar=`set_rcvar`
start_cmd="demon_start"
stop_cmd=":"

load_rc_config $name

demon_start()
{
    if checkyesno ${rcvar}; then
      /usr/home/steph/demon/demon.sh
    fi
}

run_rc_command "$1"

But when I start that, it hangs, and I have to kill it with Ctrl+C. I can add a "&" after /usr/home/steph/demon/demon.sh, but in that case, I cannot stop the program using "/etc/rc.d/demon stop".

Also, is there a good way to make another user to launch the daemon (I've thought of su - steph -c "/usr/home/steph/demon/demon.sh", is there another way ?)

Thanks to everyone.
 
Hm, I have never scripted for the rc.d framework, but for a proper exit I think you need to at least specify a pidfile).
Why do you need an easy way to stop find?
I would use cron for this.
Code:
# crontab -e
SHELL=/bin/sh
PATH=/bin:/usr/bin:/usr/home/steph/bin
*/10    *    *    *    * demon.sh
A problem I could think of is if find would take more than 10 minutes to finish, the processes will overlap each other.

Not related, but consider use /usr/local/etc/rc.d instead. Separating user definied scripts/settings from the systems make things easier to manage in the long run.
 
You should include a check to determine if your script is running or not.

Just for fun;
I tried this simple setup with the rc.d framework:

/usr/local/etc/rc.d/testd
Code:
#!/bin/sh

. /etc/rc.subr

name=testscript
rcvar=testscript_enable

command="/root/bin/${name}"

load_rc_config $name
run_rc_command "$1"

/root/bin/testscript
Code:
#!/bin/sh
while true
do
    echo "script running" > /tmp/script
    sleep 1
done

Code:
# echo 'testscript_enable="YES"' >> /etc/rc.conf
# /usr/local/etc/rc.d/testd
Starting testscript.

It doesn't hang. It just doesn't put the script in the background. Their are probably proper ways to do this.
 
> It doesn't hang. It just doesn't put the script in the background.

That's why I sait my english was bad, that's exactly what I meant.
 
Back
Top