I am running OpenVPN client on my dedicated server to connect the server to a private network. Last week, the service (i.e. OpenVPN) disconnected itself and we did not realise until our customers started to ring us. I don't know the reason why it disconnected, Now I want to set up email alert as soon as the OpenVPN disconnects. How can I do that ? Can anyone help please.

By the way, the server is running CentOS 6.8

Thank you.

  • There are so many monitoring solutions to chose from ... – Gerald Schneider Dec 7 '16 at 9:32
  • I know I can use Monit, Pingdom, SIM (System Integrity Monitor), Host-Tracker, Nagios etc. But I am looking for a simple script locally!! – AL̲̳I Dec 7 '16 at 9:44

I have successfully set up one myself with these three steps

  1. Follow this or find your own guide to configure the script for email notification

  2. Open /etc/openvpn/server.conf and append the following lines

    script-security 2
    client-disconnect /path/to/script
  3. Finally chmod 755 /path/to/script

Edit: Sorry, I misread your question. Add the two lines below to client.conf. This method is preferable over cronjob as it is almost instantaneous and doesn't flood you with mail

    script-security 2
    down /path/to/script
  • Your answer is correct but I am not running the OpenVPN server, I am only running client, please see my answer – AL̲̳I Dec 15 '16 at 11:50

I have managed to write a simple script to check if the service is running, if not, restart the service and send email. Here is the simple code

filename checkprocess.sh


# timestamp
ts=`date +%m-%d-%Y_%H:%M:%S`

if pgrep "openvpn" > /dev/null
    echo "$ts: OpenVPN running..." >> /etc/openvpn/myscript/processcheck.log
    echo "$ts: openvpn not running, restarting..." >> /etc/openvpn/myscript/processcheck.log 
    /etc/init.d/openvpn start > /dev/null >> /etc/openvpn/myscript/processcheck.log

    if pgrep "openvpn" > /dev/null
        echo "$ts: openvpn started OK..." >> /etc/openvpn/myscript/processcheck.log
        echo "OpenVPN service went down, the service has been restarted." | mail -s "OpenVPN went down" email@domain.com
        echo "$ts: openvpn not started..." >> /etc/openvpn/myscript/processcheck.log
        echo "OpenVPN service is down, the server is trying to restart the service." | mail -s "OpenVPN is down" email@domain.com

put this file in /etc/openvpn/myscript and edit the cronjob

#crontab -e 

Add the following line to run the script every 5 minutes

*/5 * * * * /etc/openvpn/myscript/checkprocess.sh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.