wewa
wewa

Reputation: 1678

systemd: Pass start/stop to service

I am trying to create an systemd init script for starting and stopping the softether VPN server.

A tutorial I found suggests following init.d script.

#!/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

But I'd like to use systemd, so I wrote following service file.

[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

But this script does not keep the VPN server running. sudo systemctl status softethervpn gives me following status.

● 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.

How do I need to correct my service file to work correctly?

Upvotes: 3

Views: 1804

Answers (2)

Shakiba Moshiri
Shakiba Moshiri

Reputation: 23774

for SoftEther this works

[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

It is the official service for SoftEther, expect this line has been removed

CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_BROADCAST CAP_NET_RAW CAP_SYS_NICE CAP_SYSLOG CAP_SETUID

which caused some error for me, Ex,

 -- Alert: SoftEther VPN Kernel --
 Unable to create /usr/local/vpnserver/.VPN-49BDCFFA14.
 -- Alert: SoftEther VPN Kernel --
 Unable to create /usr/local/vpnserver/.VPN-49BDCFFA14.

Upvotes: 0

wewa
wewa

Reputation: 1678

It seems that the Type needs to be forking. Following script works for me (found at 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

Upvotes: 3

Related Questions