我最近将在虚拟机 (Debian 7.8) 上运行的本地服务迁移到物理计算机 (Debian 8.5),迁移后,我的初始化脚本停止工作。我知道 Wheezy 和 Jessie 之间的 init 进程等发生了一些重大变化,但据我所知,这些更改应该仍然与我的 init 脚本兼容。我很确定我按照上面的说明对脚本进行了“LSBized”Debian 维基无论如何,仍然非常接近旧的骨架示例。
我做了很多尝试,试图弄清楚发生了什么,我很确定脚本(通常?)正在崩溃 - 或者至少停止 - 在/lib/lsb/init-functions
.我不知道为什么会发生这种情况。
得出这个结论后,我将 init 脚本修剪为一个(相当笨拙)较短的版本,不使用 init 函数;它只是将 bash 进程分叉到子子 shell 中,因此它将被孤立。这似乎效果更好一些,但新服务设置仍然偶尔会出现问题。我对修复这个管道胶带和捆扎线解决方案不太感兴趣,因为我正在弄清楚为什么我的更标准的初始化脚本行为不当。
有任何想法吗?
这是我的旧初始化脚本:
#! /bin/sh
### BEGIN INIT INFO
# Provides: myd
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Initscript for myService daemon
### END INIT INFO
# Do NOT "set -e"
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="myService daemon"
NAME=myd
DAEMON=/home/uname/myService/bin/$NAME
DAEMON_ARGS=""
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh
# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions
#
# Function that starts the daemon/service
#
do_start()
{
# Return
# 0 if daemon has been started
# 1 if daemon was already running
# 2 if daemon could not be started
start-stop-daemon --start --quiet \
--background \
--pidfile $PIDFILE \
--make-pidfile \
--user uname \
--chuid uname \
--startas /bin/bash \
--test \
-- -c "exec $DAEMON >> /.myd/var/init.log 2>&1" \
|| return 1
start-stop-daemon --start --quiet \
--background \
--pidfile $PIDFILE \
--make-pidfile \
--user uname \
--chuid uname \
--startas /bin/bash \
-- -c "exec $DAEMON >> /home/uname/.myd/var/init.log 2>&1" \
|| return 2
# Add code here, if necessary, that waits for the process to be ready
# to handle requests from services started subsequently which depend
# on this one. As a last resort, sleep for some time.
return 0
}
#
# Function that stops the daemon/service
#
do_stop()
{
# Return
# 0 if daemon has been stopped
# 1 if daemon was already stopped
# 2 if daemon could not be stopped
# other if a failure occurred
start-stop-daemon --stop --quiet \
--retry=TERM/30/KILL/5 \
--pidfile $PIDFILE \
--name $NAME
RETVAL="$?"
[ "$RETVAL" = 2 ] && return 2
# Wait for children to finish too if this is a daemon that forks
# and if the daemon is only ever run from this initscript.
# If the above conditions are not satisfied then add some other code
# that waits for the process to drop all resources that could be
# needed by services started subsequently. A last resort is to
# sleep for some time.
start-stop-daemon --stop --quiet --oknodo \
--retry=0/30/KILL/5 \
--exec $DAEMON
[ "$?" = 2 ] && return 2
# Many daemons don't delete their pidfiles when they exit.
rm -f $PIDFILE
return "$RETVAL"
}
#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
#
# If the daemon can reload its configuration without
# restarting (for example, when it is sent a SIGHUP),
# then implement that here.
#
start-stop-daemon --stop --signal 1 --quiet \
--pidfile $PIDFILE \
--name $NAME
return 0
}
case "$1" in
start)
log_daemon_msg "Starting $DESC" "$NAME"
do_start
case "$?" in
0|1) log_end_msg 0
;;
2) log_end_msg 1
;;
esac
;;
stop)
log_daemon_msg "Stopping $DESC" "$NAME"
do_stop
case "$?" in
0|1) log_end_msg 0
;;
2) log_end_msg 1
;;
esac
;;
status)
status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
;;
#reload|force-reload)
#
# If do_reload() is not implemented then leave this commented out
# and leave 'force-reload' as an alias for 'restart'.
#
#log_daemon_msg "Reloading $DESC" "$NAME"
#do_reload
#log_end_msg $?
#;;
restart|force-reload)
#
# If the "reload" option is implemented then remove the
# 'force-reload' alias
#
log_daemon_msg "Restarting $DESC" "$NAME"
do_stop
case "$?" in
0|1)
do_start
case "$?" in
0) log_end_msg 0 ;;
1) log_end_msg 1 ;; # Old process is still running
*) log_end_msg 1 ;; # Failed to start
esac
;;
*)
# Failed to stop
log_end_msg 1
;;
esac
;;
*)
#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
exit 3
;;
esac
:
我的新(笨拙)脚本(大部分)有效:
#! /bin/sh
### BEGIN INIT INFO
# Provides: myd
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Initscript for myService daemon
### END INIT INFO
# Do NOT "set -e"
# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="myService daemon"
NAME=myd
DAEMON=/home/uname/myService/bin/$NAME
DAEMON_ARGS=""
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0
#
# Function that starts the daemon/service
#
do_start()
{
(
runuser -l uname "$DAEMON" >/dev/null 2>&1 &
echo "$!" > "$PIDFILE"
)
return 0
}
#
# Function that stops the daemon/service
#
do_stop()
{
PID="$(cat $PIDFILE)"
kill "$PID" >/dev/null 2>&1
#Give myd 5 seconds for an orderly shutdown
for i in $(seq 1 5); do
sleep 1
if [ ! kill -0 "$PID" >/dev/null 2>&1 ]; then
rm "$PIDFILE"
return 0;
fi
done
#No more playing nice: KILL THE DAEMON
if [ kill -0 "$PID" >/dev/null 2>&1 ]; then
echo "Orderly shutdown failed, sending kill signal"
kill -9 "$PID" >/dev/null 2>&1
fi
#Give myd 5 more seconds then fail
for i in $(seq 1 5); do
sleep 1
if [ ! kill -0 "$PID" >/dev/null 2>&1 ]; then
rm "$PIDFILE"
return 0;
fi
done
#FAILED
echo "Failed to kill $DESC $NAME"
return 1
}
case "$1" in
start)
echo "Starting $DESC $NAME" >&2
do_start
;;
stop)
echo "Stopping $DESC $NAME" >&2
do_stop
;;
restart|force-reload)
#
# If the "reload" option is implemented then remove the
# 'force-reload' alias
#
echo "Restarting $DESC $NAME" >&2
do_stop
do_start
;;
*)
#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
exit 3
;;
esac
:
顺便说一句,经过多次谷歌搜索和令人头疼的事情之后,我仍然不太了解用于依赖项启动等的新系统。有人可以为我指出一些(当前)文档的方向,以便我可以自学好一点?我讨厌对我正在使用的系统一无所知。
谢谢!
答案1
您可能应该考虑转换为本机 systemd 单元,因为您的 init 脚本相当典型,例如,您可以完全删除停止代码并让 systemd 的默认机制为您工作,因为您有一个 PIDFILE,它可以在其中检查守护进程是否在正在运行,因此知道如何杀死它(请参阅 参考资料man systemd.kill
)。
systemd 有一个很大的学习曲线,但你可以从这个博客由 Lennart Poettering 本人转换为 systemd。
通常,由于内置的 systemd 兼容性,您的初始化脚本应该继续工作。在 中查看man systemd-sysv-generator
并查找脚本的 systemd 单元文件包装器。myd
/run/systemd/generator.late/myd.service
检查脚本的状态和日志
sudo systemctl status myd
并以类似的方式停止和启动它。请注意陷阱我在中描述了这个答案对 systemd 认为已经启动的事物进行第二次启动将不会产生任何效果。
看这个维基用于将 System V 或像您这样的新贵脚本转换为本机 systemd 单元。