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

3
我正在尝试创建一个systemd init脚本来启动和停止softether VPN服务器。
我找到的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.

我需要如何修改我的服务文件才能正常工作?

尝试启用并启动守护进程 sudo systemctl enable softethervpn.servicesudo systemctl start softethervpn.service,然后检查状态。 - GAD3R
这正是我所做的。此外,sudo systemctl enable softethervpn仅启用服务在启动时自动启动。 - wewa
2个回答

3

看起来需要将 Type 设置为 forking。以下脚本适用于我(在 Systemd 的 SoftEther 配置文件中找到)。

[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

我也遇到了同样的问题,在尝试了很多不同的方法之后,我终于找到了这篇文章,解决方案就是如此简单:Type=forking... 感谢您的分享! - Bitcoin Murderous Maniac

0

对于SoftEther,这个可以工作

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

[Service]
Type=forking
TasksMax=infinity
EnvironmentFile=-/usr/local/vpnserver
ExecStart=/usr/local/vpnserver/vpnserver start
ExecStop=/usr/local/vpnserver/vpnserver stop
KillMode=process
Restart=on-failure

# Hardening
PrivateTmp=yes
ProtectHome=yes
ProtectSystem=full
ReadOnlyDirectories=/
ReadWriteDirectories=-/usr/local/vpnserver

[Install]
WantedBy=multi-user.target

这是SoftEther的官方服务,期望这行已被移除

CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_BROADCAST CAP_NET_RAW CAP_SYS_NICE CAP_SYSLOG CAP_SETUID

造成了一些错误,例如,
 -- Alert: SoftEther VPN Kernel --
 Unable to create /usr/local/vpnserver/.VPN-49BDCFFA14.
 -- Alert: SoftEther VPN Kernel --
 Unable to create /usr/local/vpnserver/.VPN-49BDCFFA14.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接