CRON restart OpenVPN using kill pid


  • Banned

    I'm currently restarting my openvpn client via cron every night using: /usr/local/sbin/pfSsh.php playback svc start openvpn client 1

    However, I'm running into issues where this method doesn't update the PID with the GUI so all of the GUI interfaces say that openvpn is down even though it is working. If I leave it doing these updates for awhile then I'll see multiple PIDs in /var/run for openvpn client and server.

    All of this can be fixed by going into the shell and running: ps aux | grep openvpn then running kill "PID" for the processes and it's good to go.

    So instead of dealing with all of that, I'd like to just have CRON do that every night instead of restarting the way that I'm doing it now. But I'm not sure how to do it.

    I'd like something along the lines of this, but it doesn't work (I found it on a linux thread):

    kill $(ps aux | grep openvpn | awk '{print $2}')

    /usr/local/sbin/pfSsh.php playback svc start openvpn client 1

    I'm not sure if the "/usr/local/sbin/pfSsh.php playback svc start openvpn client 1" command is necessary, will openvpn just restart itself if I kill the PID? If not is this the best way to start openvpn after I've killed it by PID or is there a better way?

    Also, the: "kill $(ps aux | grep openvpn | awk '{print $2}')" command doesn't work (probably because it's written for linux). But basically I want to Kill the PIDs that are returned from "ps aux | grep openvpn"

    Any help is greatly appreciated!



  • Why Restart openvpn Every night?

    Have you tried svc restart?


  • Banned

    Because occasionally the VPN client goes down and needs to be restarted. If it goes down then most of network goes down because most of my network will only route through the VPN.

    Yes that's what I'm using now but that method has some known issues with the GUI that I'm trying to avoid.



  • this seems to work for me:

    
    pfSense shell: include('service-utils.inc');
    pfSense shell: service_control_restart("openvpn", array('vpnmode' => 'client', 'id' => '1'));
    pfSense shell: exec
    
    

    you can create a new macro for it & call it the same way from cron.

    you can figure out the client 'ID' by capturing the post-data when clicking the restart-button in the GUI  or from config.xml or ….



  • @heper said in CRON restart OpenVPN using kill pid:

    service_control_restart("openvpn", array('vpnmode' => 'client', 'id' => '1'));

    How can i use this? i'm a noob and i want to restart openvpn if ping fail...

    Thanks a lot
    Stefano





  • I don’t know how to use the results from Google... :(