Crontab - start random openvpn instances

If I run the commands service openvpn stop us1 or service openvpn start us1 they work fine.

I have 3 vpn instances, us1 , jp1, nl1 is it possible to randomize on cron start or would I have to always use the hard-coded one only!

current config

01 00 * * * service openvpn stop jp1
59 05 * * * service openvpn start jp1

Perhaps you meant to enter "01 00", since it would be one minute after midnight.
Wikpedia: 24-hour clock

2 Likes

Thank you.
I thought parse error at 24 meant character count number at 24 :confused: lol it was literally the number 24

Is there a way to randomize the instances, thanks again

Sorry, I'm just an end user with OpenWrt. :frowning:

1 Like

This script should do the job, but let's clarify that the selected instance will be random, not sequential.

cat << "EOF" > /root/ovpnstart.sh
#!/bin/sh
inst=$(grep -m1 -ao '[1-3]' /dev/urandom | head -n1)

if [ "$inst" == 1 ]; then

        /etc/init.d/openvpn start jp1

elif [ "$inst" == 2 ]; then

        /etc/init.d/openvpn start us1

else
        /etc/init.d/openvpn start nl1

fi
EOF
chmod 755 /root/ovpnstart.sh

Stop the service without specifying any instance.

1 0 * * * /etc/init.d/openvpn stop
59 5 * * * /root/ovpnstart.sh
3 Likes

Thank you.

I had some 30 odd instances, decided to name them as numbers to make things easier.

Then modified your script a bit to make it a little easier to accommodate future instances.

nano /root/ovpnstart.sh

#!/bin/sh
inst=$(grep -m1 -ao '[1-30]' /dev/urandom | head -n1)
/etc/init.d/openvpn start $inst

chmod 755 /root/ovpnstart.sh

should get the desired results

1 0 * * * /etc/init.d/openvpn stop
59 5 * * * /root/ovpnstart.sh

Thank you.

1 Like

This will only generate numbers 0,1,2,3 (0 would be fatal because the service will not start).
Change it to:

inst=$(grep -m1 -ao '[0-2][0-9]' /dev/urandom | head -n1 | sed 's/^0//;s/00/30/')
2 Likes

Thank you so much :pray:

Added some stuff so that on reboot/power-outage, all 30 instances enabled don't try to start. So at any given time only one instance is enabled/started.

nano /root/ovpnstart.sh

#!/bin/sh

#del all currently enabled instances
ls /etc/openvpn/*.ovpn \
| while read -r OVPN_CONF
do OVPN_ID="$(basename ${OVPN_CONF%.*} | sed -e "s/\W/_/g")"
if [ openvpn.${OVPN_ID}.enabled == '1' ]; then
    uci del openvpn.${OVPN_ID}.enabled
fi
done

#generate random
inst=$(grep -m1 -ao '[0-2][0-9]' /dev/urandom | head -n1 | sed 's/^0//;s/00/30/')

#enable random instance
uci set openvpn.${inst}.enabled="1"

#commit changes
uci commit openvpn

#start random instance
/etc/init.d/openvpn start $inst

chmod 755 /root/ovpnstart.sh
1 0 * * * /etc/init.d/openvpn stop
59 5 * * * /root/ovpnstart.sh

A better way, thanks to @pavelgl explaining it to me, is to not write unnecessarily to flash.

Only posting for people who might stumble upon this thread.

Leave the script simple

nano/vi /root/ovpnstart.sh
#!/bin/sh
inst=$(grep -m1 -ao '[0-2][0-9]' /dev/urandom | head -n1 | sed 's/^0//;s/00/30/')
/etc/init.d/openvpn start $inst

Leave all openvpn instances enabled, but disable the openvpn service(luci --> System --> Startup --> Initscripts(tab) --> Disable openvpn) by default to prevent its starting after reboot. Then insert the following into (luci --> System --> Startup --> Local Startup(tab)) before exit 0

sleep 30 && /root/ovpnstart.sh

Thus, after router reboot only one (random) openvpn instance will start.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.