How to debug rc.d scripts in FreeBSD?

2020-04-27 08:18发布

问题:

I have a bash script in my

/usr/local/etc/rc.d/

that should run python script. I run the bush script with

service script_name start

and nothing happens at all. How could i debug that rc.d script? How could i know what is going on?

回答1:

FreeBSD rc.d system expects /bin/sh scripts. Hence sh debugging techniques apply here. For example printing the statements with set -x and set -v.

# cat script.sh

#!/bin/sh
set -x
set -v
...

Below is a simple example how to start my_app with the service command

# cat /scratch/my_app

#!/usr/local/bin/bash
case $1 in
     start)
         echo "Start my_app"
         exit
         ;;
     stop)
        echo "Stop my_app"
        exit
        ;;
esac

# cat /usr/local/etc/rc.d/my_app

#!/bin/sh
#set -x
#set -v
. /etc/rc.subr
name="my_app"
rcvar=my_app_enable
load_rc_config $name
start_cmd=${name}_start
stop_cmd=${name}_stop
my_app_start() {
    /scratch/my_app start
}
my_app_stop() {
    /scratch/my_app stop
}
run_rc_command "$1"

# grep my_app /etc/rc.conf

my_app_enable="YES"

# service my_app start

Start my_app

Details are available in Practical rc.d scripting in BSD and The Design and Implementation of the NetBSD rc.d system.

Also quoting from the doc

The manual pages rc(8), rc.subr(8), and rcorder(8) document the rc.d components in great detail. You cannot fully use the rc.d power without studying the manual pages and referring to them while writing your own scripts.