首页 > 解决方案 > 如何在 FreeBSD 中调试 rc.d 脚本?

问题描述

我有一个 bash 脚本

/usr/local/etc/rc.d/

那应该运行python脚本。我运行布什脚本

service script_name start

什么也没有发生。我怎样才能调试那个 rc.d 脚本?我怎么知道发生了什么事?

标签: shellfreebsdrc.d

解决方案


FreeBSD rc.d系统需要/bin/sh脚本。因此sh调试技术适用于此。例如,使用'set -x''set -v'打印语句

shell> cat script.sh
#!/bin/sh
set -x
set -v
...

下面是一个简单的例子,说明如何使用service命令启动my_app

shell> cat /scratch/my_app
#!/usr/local/bin/bash
case $1 in
     start)
        echo "Start my_app"
        exit
        ;;
     stop)
        echo "Stop my_app"
        exit
        ;;
esac
shell> 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"
shell> grep my_app /etc/rc.conf
my_app_enable="YES"
shell> service my_app start
Start my_app

详细信息可在

还引用了文档

手册页 rc(8)、rc.subr(8) 和 rcorder(8) 详细记录了 rc.d 组件。如果不研究手册页并在编写自己的脚本时参考它们,就无法充分利用 rc.d 的力量。


推荐阅读