将socat初始化脚本迁移到systemd


8

我在sysVinit的debian 7.2上将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

但是,在升级到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

当我启动该服务时,它已启动但直接停止:

已加载:已加载(/usr/lib/systemd/system/socat.service;已启用)
活动:自2014年4月18日星期五14:09:46 +0200起处于非活动状态(已死);4秒钟前进程:5334 ExecStart = / etc / init.d / socat启动(代码=已退出,状态= 0 /成功)CGroup:名称= systemd:/system/socat.service

我想念什么吗?


我想念什么吗?是的,您实际上错过了将init.d脚本迁移到systemd :)
Piotr Dobrogost,2017年

Answers:


8

只是发现我必须使用

Type=forking

http://www.freedesktop.org/software/systemd/man/systemd.service.html中所述

如果设置为派生,则预期使用ExecStart =配置的进程将在其启动过程中调用fork()。启动完成并设置所有通信通道后,预计父进程将退出。子级继续作为主守护进程运行。这是传统UNIX守护程序的行为。如果使用此设置,建议也使用PIDFile =选项,以便systemd可以识别守护程序的主进程。一旦父进程退出,systemd将继续启动后续单元。


1

对于socat,我使用纯系统方法。这是串行回送的示例:

[Unit]
Description=Socat Serial Loopback
#Before=my-other.service

[Service]
Type=simple
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=socat-serial-lo

ExecStart=/usr/bin/socat -d -d pty,raw,echo=0,link=/tmp/seriallo-a pty,raw,echo=0,link=/tmp/seriallo-b
Restart=always

[Install]
WantedBy=multi-user.target

可以将其写入/etc/systemd/system/socat-serial-lo.service(在Ubuntu 16.04+中),然后:

systemctl daemon-reload
systemctl start socat-serial-lo
systemctl enable socat-serial-lo  # (to start it during bootup)

此方法的一个优点是,ExecStart可以直接从命令行测试定义的命令行,而无需进行更改,以便测试命令。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.