I have a script that I would like to run only if VPN is up.
I can run a job via OpenVPN when the VPN starts up and shuts down, but I would like one job to run periodically when the VPN is up, and not at all when it is down.
What is the most elegant way to achieve this?
Quick answer so the question has some closure:
The comment by @user535733 is the best way in my opinion. VPN is disabled at startup and started manually in this system, by
systemd disable openvpn
.I have the following added to the
vpn-up.sh
script which is executed after the VPN starts bysystemctl start openvpn
:The script starts and generates a file
piaport
in/etc/cron.d
which runs a script one hour after its generation, and again every hour afterwards.The
vpn-down.sh
script which is run when the VPN service is stopped bysystemctl stop openvpn
:For unexpected reboots, the script called by the cronjob checks for the existence of the VPN and deletes the cronjob
/etc/cron.d/piaport
if it was left over:part of script:
In total, the cronjob is only run when necessary, and when the VPN is up.