I would like to run a cronjob every 150 minutes. Is it possible? Any tip would be much appreciated.
-
My bad. I meant every **150** minutes. – karmatic Aug 28 '17 at 07:15
-
It might be simpler to schedule it to run every two hours. – Basile Starynkevitch Aug 28 '17 at 07:20
2 Answers
Two cron jobs:
00 1,6,11,16,21 * * * /command
30 3,8,13,18,23 * * * /command
or write a script that calls the job after 2.5 hours, and call the job and your script every 5 hours

- 50,732
- 33
- 89
- 96

- 241
- 1
- 3
-
Probably need to figure out from OP what to do with uneven distribution - there are only 1.5 hrs between `23:30` and `01:00`. – paxdiablo Aug 28 '17 at 07:31
-
Yea. If you need uneven distribution, writing a 2 line bash script with sleep 9000 and the job is better. – Sam51 Aug 28 '17 at 07:33
You can generally do this quite easily with a couple of separate crontab
entries, one for the five-hour jobs, the other for the in-between jobs:
0 0,5,10,15,20 ...
30 2,7,12,17,22 ...
keeping in mind that the gap between the last job for a day and the first job the following day is only an hour and a half (2.5 doesn't divide evenly into 24).
If you really need a two-and-a-half-hour gap, you can run your job as often as you need to but only execute the payload of said job when needed.
One way to do this is to use a state file to maintain the last time the payload was run and then only run the payload after some suitable time has passed. For example, this script will only run the payload (the final echo
) every seven seconds, regardless of how often the script is called:
#!/bin/bash
# Configuration items.
stateFile=/tmp/lastTime.txt # File holding time of last payload run.
minGap=7 # How often payload should run (seconds).
# Get last payload run time (with checks for non-valid data).
((lastTime = 0))
[[ -f ${stateFile} ]] && lastTime="$(cat ${stateFile})"
[[ "${lastTime}" =~ [1-9][0-9]* ]] || ((lastTime = 0))
# Exit if not enough time elapsed, else update last payload run time.
thisTime=$(date +%s)
((timeGap = thisTime - lastTime))
[[ ${timeGap} -lt ${minGap} ]] && exit
echo ${thisTime} >${stateFile}
# This is the payload section, put whatever code you want here.
echo "[${lastTime}] [${thisTime}] [${timeGap}]"
Assuming you've called that script runme.sh
, you can test it with:
while true ; do ./runme.sh ; sleep 1 ; done
This will run it every second but you'll notice the payload is only done every seven seconds (other than the first time), as expected:
[0] [1503908311] [1503908311]
[1503908311] [1503908318] [7]
[1503908318] [1503908325] [7]
[1503908325] [1503908332] [7]
[1503908332] [1503908339] [7]
[1503908339] [1503908346] [7]
[1503908346] [1503908353] [7]
For your particular case, you would set minGap
to be 9000 (two and a half hours, in seconds) and have cron
run the script based on the maximum amount of time you'd wish to wait after the gap has passed.
For example, let's say you're happy with a potential delay of five minutes (subsequent jobs running anywhere between 90 and 95 minutes after the preceding job). You would then set up cron
so that the script runs every five minutes.

- 854,327
- 234
- 1,573
- 1,953