我正在尝试使初始化脚本按预期工作,启动/停止功能完美地工作,但我无法使状态功能按预期工作。我正在运行: 发行商 ID:Debian 描述:Debian GNU/Linux 7.6 (wheezy) 版本:7.6 代号:wheezy
这是我的初始化脚本:
#! /bin/sh
### BEGIN INIT INFO
# Provides: graylog-collector
# Required-Start: $network $named $remote_fs $syslog
# Required-Stop: $network $named $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Graylog Collector
# Description: Graylog's open source log shipper
### END INIT INFO
# Process name ( For display )
NAME="graylog-collector"
# Daemon name, where is the actual executable
DAEMON="/etc/graylog-collector/bin/graylog-collector"
# DAEMON args
DAEMON_OPT="run -f /etc/graylog-collector/config/collector.conf"
# User to run the deamon
DAEMON_USER="graylog-collector"
# pid file for the daemon
PIDDIR="/var/run/$NAME"
PIDFILE="$PIDDIR/$NAME.pid"
PATH="/sbin:/bin:/usr/sbin:/usr/bin"
test -x $DAEMON || exit 0
. /lib/lsb/init-functions
d_start () {
log_daemon_msg "Starting system $NAME Daemon"
if [ ! -e $PIDDIR ] ; then
mkdir $PIDDIR
chown ${DAEMON_USER}:${DAEMON_USER} $PIDDIR
fi
start-stop-daemon --background --start \
--user $DAEMON_USER \
--chuid $DAEMON_USER \
--make-pidfile \
--pidfile $PIDFILE \
--startas /bin/bash -- -c "exec $DAEMON $DAEMON_OPT >> /var/log/graylog-collector/console.log 2>&1" || return 2
sleep 2
log_end_msg $?
}
d_stop () {
log_daemon_msg "Stopping system $NAME Daemon"
start-stop-daemon --stop --retry 5 --quiet --user $DAEMON_USER
rm -f $PIDFILE
log_end_msg $?
}
case "$1" in
start|stop)
d_${1}
;;
restart|reload|force-reload)
d_stop
d_start
;;
force-stop)
d_stop
killall -q $NAME || true
sleep 2
killall -q -9 $NAME || true
;;
status)
status_of_proc "${PIDFILE}" "$DAEMON" "$NAME" && exit 0 || exit $?
;;
*)
echo "Usage: /etc/init.d/$NAME {start|stop|force-stop|restart|reload|force-reload|status}"
exit 1
;;
esac
exit 0
谢谢。
更新1: 我通过创建以下函数,用一个丑陋但有效的解决方案(至少满足我的小要求)更新了脚本:
do_status () {
if { ps -U ${DAEMON_USER} ; } >/dev/null 2>&1 ; then
log_success_msg "${NAME} is running"
return 0
else
log_failure_msg "${NAME} is not running"
return $?
fi
}
然后调用它:
status)
#status_of_proc "${PIDFILE}" "$DAEMON" "$NAME" && exit 0 || exit $?
do_status
;;
答案1
根据我的理解,您应该将 -p 与“${PIDFILE}”一起使用。也就是说,除非您使用“set -e”,否则您永远不应该在 init.d 脚本中使用 -p 检查进程,我认为这是行不通的。最后试试这个:
status)
status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
;;