0

Once or twice a day, when the service I'm building is in production I'll have to read a database that sits behind a VPN.

Now, in the development phase I've been doing as follows. As root user I run:

openfortivpn -c /etc/openfortivpn/config

Then, as web user I run:

sh manage.sh sync_master

Where manage.sh is:

. /web/.env;. /web/bin/activate;/web/bin/python /web/src/manage.py $1 $2 $3

After the synchronisation happens, I go back to root user and on the screen I left the VPN connection open, I stop it using ctrl+c.

Now I need to run this from crontab. So my question is twofold: Is this a good way of doing this? How can I put all this logic in a bash script?

Thanks!

PS: Running on Ubuntu 18.04.1 LTS

  • 1
    Simply start the VPN as a service at boot and keep it up and running rather than starting and stopping every time as that seem rather fragile. – HBruijn Feb 25 at 18:07
  • That's one of the many reasons why we don't run long running services in screen. As several people have said, make an actual service of it. – Michael Hampton Feb 25 at 19:01
2
+50

Suggest to write a systemd.service that starts|stops the VPN and use systemctl within the script.

  • Indeed, this turned out to be a very easy to implement and to reason about. Thanks for pointing this out. – fceruti Feb 27 at 10:50
  • Your welcome. Good to read that you managed implementing the solution. :) – hargut Feb 27 at 17:07
2

The proper way to start/stop/restart a process in linux would be to track it by a PID file. Here is an example script you can start with. A good thing is that below script also have a varify sections, so you can put it in cron like "/path/to/scriptname varify" and it will start openvpn if it is not running.

#!/bin/bash

DAEMON_PATH="/full/path/to/"
DAEMON="/full/path/to/openfortivpn"
DAEMONOPTS="-c /etc/openfortivpn/config"

NAME=openfortivpn
DESC="openfortivpn"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

case "$1" in
start)
        printf "%-50s" "Starting $NAME... "
        echo  "Starting $NAME... " 
        cd $DAEMON_PATH
        PID=`$DAEMON $DAEMONOPTS & echo $!`
        echo "Saving PID" $PID " to " $PIDFILE
        if [ -z $PID ]; then
            printf "%s\n" "Fail"
        else
            echo $PID > $PIDFILE
            printf "%s\n" "Ok"
        fi
;;
status)
        printf "%-50s" "Checking $NAME..."
        if [ -f $PIDFILE ]; then
            PID=`cat $PIDFILE`
            if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
                printf "%s\n" "Process dead but pidfile exists"
            else
                echo "Running"
            fi
        else
            printf "%s\n" "Service not running"
        fi
;;
stop)
        printf "%-50s" "Stopping $NAME"
        echo "Stopping $NAME" 
            PID=`cat $PIDFILE`
            cd $DAEMON_PATH
        if [ -f $PIDFILE ]; then
            kill -HUP $PID
            printf "%s\n" "Ok"
            rm -f $PIDFILE
        else
            printf "%s\n" "pidfile not found"
            pkill -f "openfortivpn"
        fi
;;
varify)
        printf "%-50s" "Checking $NAME..."
        if [ -f $PIDFILE ]; then
            PID=`cat $PIDFILE`
            if [ -z "`ps axf | grep ${PID} | grep -v grep`" ]; then
                printf "%s\n" "Process dead but pidfile exists"
                echo "restarting..."
                $0 restart
            else
                echo "Running"
            fi
        else
            printf "%s\n" "Service not running"
            echo "starting..."
            $0 restart
        fi
;;

restart)
        $0 stop
        $0 start
;;

*)
        echo "Usage: $0 {status|start|stop|restart|varify}"
        exit 1
  • Wow, thanks for the response, but I gotta say, I felt a little overwhelmed with your response. I was looking for somethings simpler... – fceruti Feb 26 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.