systemd:将启动/停止传递给服务

时间:2016-04-18 17:14:32

标签: raspberry-pi debian systemd init.d

我正在尝试创建一个用于启动和停止softether VPN服务器的systemd init脚本。

我发现tutorial建议遵循init.d脚本。

#!/bin/sh
# chkconfig: 2345 99 01
# description: SoftEther VPN Server

DAEMON=/usr/local/vpnserver/vpnserver
LOCK=/var/lock/subsys/vpnserver
test -x $DAEMON || exit 0

case "$1" in
    start)
        $DAEMON start
        touch $LOCK
        ;;
    stop)
        $DAEMON stop
        rm $LOCK
        ;;
    restart)
        $DAEMON stop
        sleep 3
        $DAEMON start
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac

exit 0

但是我想使用systemd,所以我写了以下服务文件。

[Unit]
Description=Softether VPN server
After=syslog.target
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/vpnserver/vpnserver start
ExecStop=/usr/local/vpnserver/vpnserver stop

# Give a reasonable amount of time for the server to start up/shut down
TimeoutSec=300

[Install]
WantedBy=multi-user.target

但是这个脚本不会让VPN服务器继续运行。 sudo systemctl status softethervpn给了我以下状态。

● softethervpn.service - Softether VPN server
   Loaded: loaded (/lib/systemd/system/softethervpn.service; disabled)
   Active: deactivating (stop) since Mon 2016-04-18 19:11:41 CEST; 1s ago
  Process: 1463 ExecStart=/usr/local/vpnserver/vpnserver start (code=exited, status=0/SUCCESS)
 Main PID: 1463 (code=exited, status=0/SUCCESS);         : 1474 (vpnserver)
   CGroup: /system.slice/softethervpn.service
           ├─1471 /usr/local/vpnserver/vpnserver execsvc
           └─control
             └─1474 /usr/local/vpnserver/vpnserver stop

Apr 18 19:11:40 raspberrypi systemd[1]: Started Softether VPN server.
Apr 18 19:11:41 raspberrypi vpnserver[1463]: The SoftEther VPN Server service has been started.
Apr 18 19:11:42 raspberrypi vpnserver[1474]: Stopping the SoftEther VPN Server service ...
Apr 18 19:11:42 raspberrypi vpnserver[1474]: SoftEther VPN Server service has been stopped.

如何更正服务文件以使其正常工作?

1 个答案:

答案 0 :(得分:1)

似乎Type需要forking。以下脚本适合我(在SoftEther Configurationfile for Systemd找到)。

[Unit]
Description=SoftEther VPN Server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/vpnserver/vpnserver start
ExecStop=/usr/local/vpnserver/vpnserver stop

[Install]
WantedBy=multi-user.target