LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   How do I run a script every 61 minutes? (http://www.linuxquestions.org/questions/linux-software-2/how-do-i-run-a-script-every-61-minutes-808014/)

jsjones85 05-15-2010 09:59 AM

How do I run a script every 61 minutes?
 
Hi, I have a script I need to run hourly. I've looked around the net and found crontab.

The problem is, my script runs some macros I made using xmacro, which does some web surfing. So because of potential lag and whatnot, I want the script to run every 61 minutes rather than every 60 minutes. As far as I can tell, this is not possible in crontab.

Can someone please point me in the right direction?

Thank you very much.

Jeremy

pixellany 05-15-2010 10:11 AM

Similar question here:
http://www.linuxquestions.org/questi...terval-806495/

Also, search here at LQ using "crontab". There are quite a few threads.

jsjones85 05-15-2010 10:42 AM

Oops, I did do a search of "crontab" but it seems I didn't scroll down far enough. Thank you for the link.

Jeremy

colucix 05-15-2010 01:29 PM

Hi Jeremy and welcome to LinuxQuestions!

To run a script every 61 minutes, you're forced to set a cron job that runs every minute. In fact, if the first run is at midnight, only after 61 days it will run at midnight again. In this 61-days interval it happens to run at all possible hour and minute pairs. Said that, the crontab entry must be
Code:

* * * * * /path/to/script.sh
Following the discussion in the thread linked by pixellany, inside the script you can try to set-up a check to see if the difference between the current date and a reference date of your choice is a multiple of 61 minutes. For example (using BASH)
Code:

#!/bin/bash
reftime=$(date -u -d "20100515" +%s)
timenow=$(date -u -d "$(date -u +"%Y%m%d %H:%M")" +%s)
if [ $(( ($timenow - $reftime) % 3660 )) -ne 0 ]
then
  exit
fi
<your code here>

In this example the main code will run every 61 minutes starting on 15-May-2010 00:00 UTC. Calculations are made in UTC to avoid weird results at the passage from/to Daylight Saving Time.

catkin 05-15-2010 02:05 PM

If the computer is running all the time there could be a single cron job to run at, say, midnight and it could use the at command to schedule itself to be run again in 61 minutes.

MTK358 05-15-2010 02:54 PM

Code:

while true
do
  # do stuff here every 61 minutes
  sleep 61m
done

The thing is that it won't really be run every 61 minutes, it will be run every 61 minutes + the time it takes to do what you wanted to do.

jsjones85 05-16-2010 07:20 AM

Thank you everyone! Your replies have been very useful.


All times are GMT -5. The time now is 11:05 PM.