Los*_*OWL 9 debian init-script systemd sysvinit
我在 debian 7.2 和 sysVinit 上使用 socat 和以下 init 脚本。它完美地工作:
#!/bin/bash
DESC=socat
DAEMON=/usr/bin/socat
LIB=/usr/lib/socat
SOCAT_ARGS="-d -d -lf /var/log/socat.log"
[ ! -f /etc/default/socat.conf ] || . /etc/default/socat.conf
. /lib/lsb/init-functions
PATH=/bin:/usr/bin:/sbin:/usr/sbin
[ -x $DAEMON ] || exit 0
#
# Try to increase the # of filedescriptors we can open.
#
maxfds () {
[ -n "$SOCAT_MAXFD" ] || return
[ -f /proc/sys/fs/file-max ] || return 0
[ $SOCAT_MAXFD -le 4096 ] || SQUID_MAXFD=4096
global_file_max=`cat /proc/sys/fs/file-max`
minimal_file_max=$(($SOCAT_MAXFD + 4096))
if [ "$global_file_max" -lt $minimal_file_max ]
then
echo $minimal_file_max > /proc/sys/fs/file-max
fi
ulimit -n $SOCAT_MAXFD
}
start_socat() {
start-stop-daemon --quiet --start \
--pidfile /var/run/socat.$NAME.pid \
--background --make-pidfile \
--exec $DAEMON -- $SOCAT_ARGS $ARGS < /dev/null
}
stop_socat() {
start-stop-daemon --stop --quiet --pidfile /var/run/socat.$NAME.pid --exec $DAEMON
rm -f /var/run/socat.$NAME.pid
}
start () {
echo "Starting $DESC:"
maxfds
umask 027
cd /tmp
if test "x$AUTOSTART" = "xnone" -o -z "x$AUTOSTART" ; then
echo "Autostart disabled."
exit 0
fi
for NAME in $AUTOSTART ; do
ARGS=`eval echo \\\$SOCAT_$NAME`
echo $ARGS
start_socat
echo " $NAME $ARGS"
done
return $?
}
stop () {
echo -n "Stopping $DESC:"
for PIDFILE in `ls /var/run/socat.*.pid 2> /dev/null`; do
NAME=`echo $PIDFILE | cut -c16-`
NAME=${NAME%%.pid}
stop_socat
echo -n " $NAME"
done
}
case "$1" in
start)
log_daemon_msg "Starting socat" "socat"
if start ; then
log_end_msg $?
else
log_end_msg $?
fi
;;
stop)
log_daemon_msg "Stopping socat" "socat"
if stop ; then
log_end_msg $?
else
log_end_msg $?
fi
;;
reload|force-reload|restart)
log_daemon_msg "Restarting socat" "socat"
stop
if start ; then
log_end_msg $?
else
log_end_msg $?
fi
;;
*)
echo "Usage: /etc/init.d/$NAME {start|stop|reload|force-reload|restart}"
exit 3
;;
esac
exit 0
Run Code Online (Sandbox Code Playgroud)
然而,在升级到 debian 7.4 后,系统更改为 systemd。因此,为了在 systemd 上运行相同的脚本,我添加了一个包装 /etc/init.d/socat 脚本的服务:
[Unit]
Description=Socat
[Service]
ExecStart=/etc/init.d/socat start
ExecStop=/etc/init.d/socat stop
[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)
当我启动服务时,它已启动但直接停止:
已加载:已加载(/usr/lib/systemd/system/socat.service;已启用)
活动:自 2014 年 4 月 18 日星期五 14:09:46 +0200 起处于非活动状态(死);4s前进程:5334 ExecStart=/etc/init.d/socat start (code=exited, status=0/SUCCESS) CGroup: name=systemd:/system/socat.service
我错过了什么吗?
刚刚发现我必须使用
Type=forking
Run Code Online (Sandbox Code Playgroud)
如http://www.freedesktop.org/software/systemd/man/systemd.service.html 中所述。
If set to forking, it is expected that the process configured with ExecStart= will call fork() as part of its start-up. The parent process is expected to exit when start-up is complete and all communication channels are set up. The child continues to run as the main daemon process. This is the behavior of traditional UNIX daemons. If this setting is used, it is recommended to also use the PIDFile= option, so that systemd can identify the main process of the daemon. systemd will proceed with starting follow-up units as soon as the parent process exits.