Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


share|improve this question

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

0 0 */4 * * command_to_restart_openvpn

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

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.

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


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).

share|improve this answer

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

service openvpn start
while sleep $((60 * 60 * 24 * 4)); do
  service openvpn restart

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

share|improve this answer

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.