SOLVED Restart openvpn daily

Status
Not open for further replies.

ZodiacUHD

Patron
Joined
Aug 28, 2015
Messages
226
Hello everyone,
I looked through the forum but i really didn't get how to setup a cronjob or a script to restart a service (or a jail). I set up transmission with openvpn and PIA, when the connection with the VPN is lost, everything stops. Since i'm not always able to access my NAS, i'd like to setup a script to restart transmission and openvpn on a daily basis.

How can i do it?

Also, i'd like to restart daily a jail running ombi (since for reasons beyond my knowledge, it stops working every once in a while).


Thank you very much!
 

toadman

Guru
Joined
Jun 4, 2013
Messages
619
Maybe add a line to touch a dummy file? "touch \tmp\last_script_run_time" (or something like that) That way you know the last time the script ran by checking the access time on the dummy file.
 

Inxsible

Guru
Joined
Aug 14, 2017
Messages
1,123
You should maybe investigate why your connection drops. I had this problem with my VPN provider and I kept bugging them and they helped me (sort of). I tried 7 different servers until I found one that was relatively much more stable. Now instead of getting my connection dropped every 20 mins, it happens once in a few days.

Your customer service from PIA, should be able to help some.
 
Joined
Apr 9, 2015
Messages
1,258
Rather than restarting the jail it may be easier to just restart the offending service.

Create a shell script, let's say calling it restart.sh

We need to know the name of the jail exactly so you will have to look it up but if you used the plugin for transmission it would be something like "transmission_1"

Inside the script you will have something like this

Code:
jexec transmission_1 service openvpn restart


Make sure the .sh is executable and then set a cron job in FreeNAS to run the file. Should work without a problem and will restart the openvpn connection, to test just open up a shell or SSH and type the command in EXACTLY without actually logging into the jail.

But in all honesty the connection should just stay up once it's running, it could be something anywhere in between that is causing an issue but it is worth trying to figure out.
 

colmconn

Contributor
Joined
Jul 28, 2015
Messages
174
You could conditionally restart the VPN service depending on whether you can send over it (this assumes you've got firewall rules to prevent any data flowing to the internet through routes other than through the VPN provider) Something like the following is what I use from cron (run every 5 mins):

Code:
#!/bin/sh
																																	
if ! nc -z -w 1 www.google.com 80 > /dev/null 2>&1 ; then																					
		/usr/sbin/service openvpn restart																									
		sleep 10																															
		/usr/sbin/service ntpd	restart																									
		# echo /usr/sbin/service ntpdate restart		
fi
 

Inxsible

Guru
Joined
Aug 14, 2017
Messages
1,123
I had to remove my kill-switch in order for the connection to be able to reconnect if and when it dropped. With the kill-switch in place, it wouldn't reconnect until I reboot my router. When the connection drops, I have seen that sometimes, it will re-connect with the VPN service in about 10 mins (provided I don't have any kill-switch). I know that I am without internet for those 10 mins, but it's better than having to manually reboot the router.

Although, I am still looking for a better solution as in not having the connection drop at all. I think it could be because of my WNR3500L router (through which I have the VPN set up) and the fact that the processor is some puny little Broadcom. I intend to use pfSense on a much stronger box and I hope that these connection losses would go away completely. If not, then I might be looking for other answers too. :(
 

ZodiacUHD

Patron
Joined
Aug 28, 2015
Messages
226
Rather than restarting the jail it may be easier to just restart the offending service.

Create a shell script, let's say calling it restart.sh

We need to know the name of the jail exactly so you will have to look it up but if you used the plugin for transmission it would be something like "transmission_1"

Inside the script you will have something like this

Code:
jexec transmission_1 service openvpn restart


Make sure the .sh is executable and then set a cron job in FreeNAS to run the file. Should work without a problem and will restart the openvpn connection, to test just open up a shell or SSH and type the command in EXACTLY without actually logging into the jail.

But in all honesty the connection should just stay up once it's running, it could be something anywhere in between that is causing an issue but it is worth trying to figure out.

Thank you very much. I Know already that it is my VPN provider that every couple of weeks has issues with one server i might be connected to and therefore my connection closes. Since i set up a killswitch, i do not mind when it happens. I just want an easy way to work around that, like the way you just explained to me.

I tried it and it works, cheers!
 
Status
Not open for further replies.
Top