在Debian 7(Wheezy)的nginx初始化脚本中,我阅读了以下exerpt:
status) status_of_proc -p /var/run/$NAME.pid "$DAEMON" nginx && exit 0 || exit $? ;;
这段代码运行得很好, sudo service nginx status输出[ ok ] nginx is running 。 然而, status_of_proc没有在bash中定义,在短划线中也没有定义:
$ type status_of_proc status_of_proc: not found
虽然如果我把相同的支票插入nginx脚本,我得到了以下结果:
status_of_proc is a shell function
而对init文件本身运行bash提供了进一步的解释:
status_of_proc is a function status_of_proc () { local pidfile daemon name status OPTIND; pidfile=; OPTIND=1; while getopts p: opt; do case "$opt" in p) pidfile="$OPTARG" ;; esac; done; shift $(($OPTIND - 1)); if [ -n "$pidfile" ]; then pidfile="-p $pidfile"; fi; daemon="$1"; name="$2"; status="0"; pidofproc $pidfile $daemon > /dev/null || status="$?"; if [ "$status" = 0 ]; then log_success_msg "$name is running"; return 0; else if [ "$status" = 4 ]; then log_failure_msg "could not access PID file for $name"; return $status; else log_failure_msg "$name is not running"; return $status; fi; fi }
然而,将相同的函数调用插入到我自己创build的初始化脚本中,返回该函数未定义。 所以init脚本没什么特别之处。 在init脚本中也没有声明过。 我在网上读到,这是LSB的一部分,但我不明白怎么称呼它。 有人请帮我弄清楚如何使用这个奇妙的function?
我发现该函数源自nginx初始化脚本中的/lib/lsb/init-functions 。 因此补充说:
. /lib/lsb/init-functions
我的初始化脚本解决了这个问题。