Practical rc scripting very short tutorial

First make make daemon which will print "Hello World" each few seconds on the screen.
A file /usr/local/etc/myservice2 , chmod 755 with following content:
Code:
#!/usr/local/bin/mksh
echo "Starting"
while true do
    echo "Hello World"
    sleep 6
done
Then we create the rc-script to stop and start this service:
A file /usr/local/etc/myservice, chmod 755 with following content:

Code:
#!/bin/sh

# PROVIDE: myservice
# REQUIRE:
# KEYWORD:

. /etc/rc.subr

name="myservice"
rcvar=myservice_enable
start_cmd="${name}_start"
stop_cmd="${name}_stop"
pidfile="/var/run/${name}.pid"
command="/usr/local/etc/rc.d/myservice2"

load_rc_config $name
: ${myservice_enable:=NO}


myservice_start()
{
if [ -f $pidfile ]
then
    echo "ALREADY STARTED"
else
    /usr/sbin/daemon -p $pidfile ${command}
    echo "Myservice started."
fi
}

myservice_stop()
{
if [ -f $pidfile ]
then   
    kill -9 `cat $pidfile`
    echo "My service stopped"
else
    echo "NOT YET STARTED"
fi
}

run_rc_command "$1"

Done.

To start&stop the service:
Code:
service myservice onestart
service myservice onestop

You can edit /etc/rc.conf to start it automatic
Code:
myservice_enable="YES"
 
Last edited:
is this correct?
command="/usr/local/etc/rc.d/myservice2"
Yes that is correct.
FreeBSD keeps one /etc for the system and one under /usr/local for the applications. That way the two can not interfere and system upgrades are way less likely to break applications as well as user applications breaking the system upon update.
 
is this correct?
command="/usr/local/etc/rc.d/myservice2"
No, that should be /usr/local/etc/myservice2 because that's the 'command' the rc.d(8) script is suppose to execute.

A file /usr/local/etc/myservice, chmod 755 with following content:
That should be /usr/local/etc/rc.d/myservice.

You can edit /etc/rc.conf to start it automatic
Code:
my_service_enable="YES"
No, rcvar is set to myservice_enable, thus it has to be myservice_enable="YES".
 
is this correct?
command="/usr/local/etc/rc.d/myservice2"
Yes that is correct.
FreeBSD keeps one /etc for the system and one under /usr/local for the applications. That way the two can not interfere and system upgrades are way less likely to break applications as well as user applications breaking the system upon update.

Sure, /usr/local/etc/rc.d is fine, but where did the 'myservice2' come from, when it's 'myservice' being defined?
 
Also, not sure why the script (myservice2) is proposed inside /usr/local/etc, rather than /usr/local/bin.
My preference is to keep all (user based) executables under /usr/local/bin and (user based) configurations under /usr/local/etc
 
Next time i put myservice2 in /usr/local/bin/

Next time perhaps don't use two names as confusingly similar as myservice{,2}?

The daemon could be called (say) myhellod and live in /usr/local/sbin as hier(7) specifies for user-run daemons?

With the rc script therefore in /usr/local/etc/rc.d?

Unless I was the only one confused ...
 
Back
Top