Initial systemd .service

parent e5caec6b
#!/bin/sh
# Copyright (C) 2014 Alberto Gonzalez Iniesta <agi@inittab.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# This scripts tries to mimic the behaviour of the SysV init script
# in systemd. It's an alternative to declaring an instance for each
# VPN you want to start on boot
#
echo "$*" > /tmp/ovpn
test $DEBIAN_SCRIPT_DEBUG && set -v -x
DAEMON=/usr/sbin/openvpn
CONFIG_DIR=/etc/openvpn
test -x $DAEMON || exit 0
test -d $CONFIG_DIR || exit 0
# Source defaults file; edit that file to configure this script.
AUTOSTART="all"
STATUSREFRESH=10
STATUS=0
VPNS=""
if test -e /etc/default/openvpn ; then
. /etc/default/openvpn
fi
# Nothing to do if no VPN is set in AUTOSTART
if test "x$AUTOSTART" = "xnone" -o -z "$AUTOSTART" ; then
exit 0
fi
# Create list with VPNs to be managed
if test -z "$AUTOSTART" -o "x$AUTOSTART" = "xall" ; then
# all of them
for CONFIG in `cd $CONFIG_DIR; ls *.conf 2> /dev/null`; do
NAME=${CONFIG%%.conf}
VPNS="$VPNS $NAME"
done
else
# only manage specified VPNs
for NAME in $AUTOSTART ; do
[ -e "${CONFIG_DIR}/${NAME}.conf" ] && VPNS="$VPNS $NAME"
done
fi
case "$1" in
start)
for NAME in $VPNS ; do
systemctl start openvpn@${NAME}
[ "$?" -ne 0 ] && { echo "Error starting $NAME"; STATUS=1; }
done
;;
stop)
for NAME in $VPNS ; do
systemctl stop openvpn@${NAME}
[ "$?" -ne 0 ] && { echo "Error stopping $NAME"; STATUS=1; }
done
;;
# Only 'reload' running VPNs. New ones will only start with 'start' or 'restart'.
reload)
for NAME in $VPNS ; do
# If openvpn if running under a different user than root we'll need to restart
if egrep '^[[:blank:]]*user[[:blank:]]' $CONFIG_DIR/$NAME.conf > /dev/null 2>&1 ; then
systemctl restart openvpn@${NAME}
[ "$?" -ne 0 ] && { echo "Error restarting $NAME"; STATUS=1; }
else
systemctl reload openvpn@${NAME}
fi
done
;;
restart)
for NAME in $VPNS ; do
systemctl restart openvpn@${NAME}
[ "$?" -ne 0 ] && { echo "Error stopping $NAME"; STATUS=1; }
done
;;
cond-restart|soft-restart)
for NAME in $VPNS ; do
systemctl condrestart openvpn@${NAME}
[ "$?" -ne 0 ] && { echo "Error stopping $NAME"; STATUS=1; }
done
;;
*)
echo "Usage: $0 {start|stop|reload|restart|cond-restart}" >&2
exit 1
;;
esac
exit $STATUS
# vim:set ai sts=2 sw=2 tw=0:
debian/openvpn@.service /lib/systemd/system
debian/openvpn.conf /usr/lib/tmpfiles.d
debian/openvpn.service /lib/systemd/system
debian/openvpn-systemd-helper /usr/sbin
[Unit]
Description=OpenVPN service
After=network.target
[Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/sbin/openvpn-systemd-helper start
ExecStop=/usr/sbin/openvpn-systemd-helper stop
ExecReload=/usr/sbin/openvpn-systemd-helper reload
WorkingDirectory=/etc/openvpn
[Install]
WantedBy=multi-user.target
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment