1

I would like to know if there is a easy way to schedule an application such as OpenVPN running on a Linux server to automatically restart every 4 days at a specific time.

Thanks

4 Answers 4

2

Use cron. Place a file in /etc/cron.d containing the following line :

0 0 */4 * * command_to_restart_openvpn

3
  • Which works fine except at the end of the month when the interval will often be shorter than four days. Nov 7, 2009 at 10:36
  • 3
    True, but is it really that important?
    – MikeyB
    Nov 7, 2009 at 17:28
  • Thanks. How do I do this.Can I use LXAdmin for this?
    – Lamboo
    Nov 7, 2009 at 23:58
1

See this answer and this one.

You can create a cron job that runs every day and checks to see if the remainder of dividing date +%s by 24*60*60 (seconds in a day) is a multiple of four. If not, then exit.

#!/bin/bash
if (( $(date +%s) / (60*60*24) % 4 != 0 )); then exit; fi

or

#!/bin/sh
if [ $(($(date +%s)/(60*60*24)%4)) != 0 ]; then exit; fi

You can change "0" to 1, 2, or 3 to affect which day in the four-day cycle is your trigger day. As noted in the first link above, this doesn't take into account leap seconds.

Or you can have your script do its thing then queue itself to run again with at now + 4 days (instead of "now" you can specify a start time: at 9:00 + 4 days to prevent time creep).

1

I have an application that needs to run every 20 days. To do that I use a script which checks the timestamp of a flag file (creating it if it doesn't exist). If that file was modified less than 20 days ago the script exits. If not, it "touches" the flag file to reset its timestamp to now and calls the application that needs to run. The script is run as a daily cron job.

0

If it really is that important to get every 4 days, run this script on startup (adapt to your local needs):

#!/bin/bash
service openvpn start
while sleep $((60 * 60 * 24 * 4)); do
  service openvpn restart
done

But you will probably find that your life will be better by restarting it via cron as Dennis suggests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.