OpenVPN restart script

Hi,

I have the following situation. 2 lines - main and backup. 2 default gateways - backup with larger metrics. It works well, but I have an OpenVPN client which does not restart when changing from main to backup line. So I basically need script to watch if the backup is on then to restart the OpenVPN interface and wait for the main line to become available again then restart the OpenVPN again. Can anyone give me an example or something?

You can also adjust OVPN keepalive timeouts.

I tried the idle timeout setting, but the timeout timer resets everytime someone tries to push traffic through the vpn, so it’s no use. And I don’t want the vpn to disconnect when there is no need

well then you can simply check gateway status and disable/enable ovpn interface

Add comments to routes for example “main” and “backup”

{
:if ([/ip route get [find comment=“backup”] active] != false) do={
:local id [/interface ovpn-client find name=“ovpn-name”];
/interface ovpn-client disable $id;
/interface ovpn-client enable $id;
}

Thanks, I think that will work.
I’ll write 2 similar scripts - one to check if backup is on and restart the VPN and one to check if main is on and restart the VPN. The second one will be disabled in scheduler. When the first one executes it will disable itself and enable the second one and vice versa.

Also add some kind of check whether vpn is already restarted once after gateway change otherwise ovpn interface will be disabled/enabled every x interval