Specifying "all odd values" in crontab? - cron

In crontab, I can use an asterisk to mean every value, or "*/2" to mean every even value.
Is there a way to specify every odd value? (Would something like "1+*/2" work?)

Depending on your version of cron, you should be able to do (for hours, say):
1-23/2
Going by the EXTENSIONS section in the crontab(5) manpage:
Ranges can include "steps", so "1-9/2" is the same as "1,3,5,7,9".
For a more portable solution, I suspect you just have to use the simple list:
1,3,5,7,9,11,13,15,17,19,21,23
But it might be easier to wrap your command in a shell script that will immediately exit if it's not called in an odd minute.

Every odd minute would be:
1-59/2 * * * *
Every even minute would be:
0-58/2 * * * *

I realize this is almost 10 years old, but I was having trouble getting 1-23/2 for an every two hour, odd hour job.
For all you users where, exact odd hour precision is not needed. I did the following which suited my teams needs.
59 */2 * * *
Execute the job every two hours, at the 59th Minute.

Try
1-23/2
From your question, I'm assuming Vixie Cron. I doubt this will work with any other cron.

As I read the manual "1-23/2" (for hours) would do the trick.

Works on Cronie
Even with 5 minutes interval e.g.
3-58/5 * * * * /home/test/bin/do_some_thing_every_five_minute

Related

Is the following cron expression means every 45 minutes?

Am willing to run a script every 45 minute (not the :45th minute of every hour)
e.g. 10:00, 10:45, 11:30, 12:15, and so on.
*/45 * * * *
Am not sure this is the correct expression.
I suspect (edit: I'm pretty sure by now) that it doesn't do what you want: fields are separate, and */45 for minutes is nothing more than 0,45. I would use the following three entries if */45 doesn't do the job:
0,45 0-23/3 * * *
30 1-23/3 * * *
15 2-23/3 * * *
If you take a look at entry.c file in vixie cron sources, you'll notice that each field of each entry is parsed by get_list and represented as bitmaps of allowed values for that field. That almost precludes any "smart" interpretation, as the distinction of */45 and 0,45 is lost at this stage... but there is a MIN_STAR flag, set at the presence of * in minutes (including */45). So we take a look at cron.c, a single place where MIN_STAR is examined, to learn it's unrelated to our problem. Now we know for sure that */45 means "every 45th minute of every hour": 0:00, 0:45, 1:00, 1:45 and so on.
There were two answers here confidently stating the opposite, quoting an unfortunate passage in the manual:
Steps are also permitted after an asterisk, so if you want to say
"every two hours", just use "*/2"
We are lucky to have a 24 hour day, containing even number of hours, making "every two hours from 0:00, each day" and "every two hours generally" indistinguishable. Too bad that the manual didn't go far enough to document non-trivial cases, making the impression that * */22 means every 22 hours. It does not. Star with a step is just a shorthand for a list of values in the field where it's used; it doesn't interact with other fields.
At the basic timing of cron, your system checks once per minute to see if there are any cronjobs to run. It will look at your crontab, and if it is time to run, poof, it runs! But every 45 minutes is an interval that will always hit at a 15 minute mark on the clock face. for example starting from zero the first is 0:45. Next will be 90 minutes from zero, or 1:30, the next will be 2:15... so easily you can see that the time for each instance you want to execute your script the minute hand will be on 12, 3, 6, or 9. If you execute your script every 15 minutes, and check the hour to see if it is the correct one, you can then execute your script. You will probably use either a table, or you might also use the modulo feature. Cheers!

Cron : Setting alternative minutes

I need to run 2 scripts alternatively and as fast as possible.
Is there a way to have one script run on the even minutes of an hour and the other on the odd minutes.
Short of writing all the minutes out by hand.
0-59/2 * * * * first_script
1-59/2 * * * * second_script
Why not just write out the minutes by hand? It will probably be clearer than trying to do anything clever - and only take about as many characters as posting this question.
I'm a big fan of "dumb" solutions which will obviously work, over trying to be clever.
Another alternative is to have one script which runs both of the others, one after another - then just run that one every other minute (0, 2, 4, ...)

Perl Cron Scheduler: start at x time, execute every y minutes forever

I'm using perl cron, and I want to make a rule like this
run every xx min/hours starting at yy:yy time (until the end of time)
How would I put this into a cron string? perl:cron seems to use the same syntax as regular cron so a regular cron string should work
TIA!
The short answer is that you will either need to write this yourself or find a different third-party package, due to your requirements. There's two things you're asking for that cron doesn't do:
Run every X minutes.
Say you want to run every 40 minutes, and you write this */40 * * * *. This actually runs every 60 minutes at 1:40, 2:40, etc.
Start/Stop at time Y/Z.
There's simply no cron syntax for this. You could use a couple more cronjobs to add/remove the main cronjob at the specified times, but this smells a lot like self-modifying code. Given the complexity (read: unreliability), it's probably better to find a different system.
You can specify intervals with a slash. Here's every 5 minutes:
*/5 * * * *
This is every 2 hours:
0 */2 * * *
You cannot give a start/ end time in cron.

Execute cronjobs in lock step

I have two cronjobs, each using a "*/5 * * * *" schedule.
What I really want is to execute them every ten minutes, but the second one 5 minutes later than the first one.
Is there an elegant way to do this?
How about:
*/10 * * * * firstcommand
5-55/10 * * * * secondcommand
This works with at least one cron daemon---Dillon's cron, which I'm the current dev of. Whether it works on Vixie cron, or fcron, or bcron, or whichever cron daemon you happen to be using, I can't say.
Have your first cron task call at, which allows you to schedule a one-off execution at a specific time.

How to do a cron job every 72 minutes

How would I get a cron job to run every 72 minutes? Or some not so pretty number like that?
Since cron runs jobs time-based, not interval-based, there's no blindingly simple way to do it. However, although it's a bit of a hack, you can set up multiple lines in crontab until you find the common denominator. Since you want a job to run every 72 minutes, it must execute at the following times:
00:00
01:12
02:24
03:36
04:48
06:00
07:12
...
As you can see, the pattern repeats every 6 hours with 5 jobs. So, you will have 5 lines in your crontab:
0 0,6,12,18 * * * command
12 1,7,13,19 * * * command
24 2,8,14,20 * * * command
36 3,9,15,21 * * * command
48 4,10,16,22 * * * command
The other option, of course, is to create a wrapper daemon or shell script that executes and sleeps for the desired time until stopped.
Use at (man at). Have your app or startup script calculate a startup time 72 minutes in the future and schedule itself to run again before it starts working.
Available on windows xp and vista too.
Here's an example for gnu/linux: at -f command.sh now + 72 minutes
You could always take the approach of triggering cron every minute, and having your script exit out immediately if it's been run more recently than 72 minutes ago.
Don't use cron...
#!/bin/sh
while [ true ]
do
sleep 4320
echo "Put your program here" &
done
You cannot directly do this from cron/crontab.
Cron jobs are run on a specific schedule, not on a specific interval.
One alternative would be to work out a schedule that approximated your "every 72 minutes" by running at midnight, 1:12, 2:24, 3:36, ..., and stretching it out to approximate hitting up at midnight. Your crontab file could specify all of these times as times to execute.
Another alternative would be to have a separate application handle the scheduling, and fire your application.
You'll need to set exactly 20 tasks for this - i.e. set one at 00:00, next one at 01:12, next one at 02:24, etc.
20 iterations make a full day.
Unfortunately, this is the only way to do it, as cron tasks are set up in a fixed schedule beforehand instead of being run, say, "after X minutes the last task was executed".
Uh I know this is long overdue, but I was looking at some scheduling issues and saw this question.
Just do this in your crontab
*/72 * * * * /home/script.sh

Resources