271

I'm looking to add a crontab entry to execute a script every 30 minutes, on the hour and 30 minutes past the hour or something close. I have the following, but it doesn't seem to run on 0.

*/30 * * * *

What string do I need to use?

The cron is running on OSX.

Darryl Hein
  • 142,451
  • 95
  • 218
  • 261

6 Answers6

541

Do:

0,30 * * * * your_command
vladr
  • 65,483
  • 18
  • 129
  • 130
189

crontab does not understand "intervals", it only understands "schedule"

valid hours: 0-23 -- valid minutes: 0-59

example #1

30 * * * * your_command

this means "run when the minute of each hour is 30" (would run at: 1:30, 2:30, 3:30, etc)

example #2

*/30 * * * * your_command

this means "run when the minute of each hour is evenly divisible by 30" (would run at: 1:30, 2:00, 2:30, 3:00, etc)

example #3

0,30 * * * * your_command

this means "run when the minute of each hour is 0 or 30" (would run at: 1:30, 2:00, 2:30, 3:00, etc)

it's another way to accomplish the same results as example #2

example #4

19 * * * * your_command

this means "run when the minute of each hour is 19" (would run at: 1:19, 2:19, 3:19, etc)

example #5

*/19 * * * * your_command

this means "run when the minute of each hour is evenly divisible by 19" (would run at: 1:19, 1:38, 1:57, 2:00, 2:19, 2:38, 2:57, 3:00 etc) crontest conducted over one hour

note: several refinements have been made to this post by various users including the author

aequalsb
  • 3,765
  • 1
  • 20
  • 21
78

Try this:

0,30 * * * * your command goes here

According to the official Mac OS X crontab(5) manpage, the / syntax is supported. Thus, to figure out why it wasn't working for you, you'll need to look at the logs for cron. In those logs, you should find a clear failure message.

Note: Mac OS X appears to use Vixie Cron, the same as Linux and the BSDs.

Eddie
  • 53,828
  • 22
  • 125
  • 145
  • I've always used */30 with success but I don't recall an application of mine where it was too critical if it only ran once an hour so I don't think I've ever checked. – Adam Hawes Feb 25 '09 at 05:39
  • 2
    @Adam Hawes: I've also used */xx with success in the past, but on Linux and other UNIXes. I've not personally tried Mac OSX. The original question didn't say which OS. Some older UNIXes don't support */xx – Eddie Feb 25 '09 at 06:03
10

If your cron job is running on Mac OS X only, you may want to use launchd instead.

From Scheduling Timed Jobs (official Apple docs):

Note: Although it is still supported, cron is not a recommended solution. It has been deprecated in favor of launchd.

You can find additional information (such as the launchd Wikipedia page) with a simple web search.

PCheese
  • 3,231
  • 28
  • 18
9

You can use both of ',' OR divide '/' symbols.
But, '/' is better.
Suppose the case of 'every 5 minutes'. If you use ',', you have to write the cron job as following:

0,5,10,15,20,25,30,35,....    *      *     *   * your_command

It means run your_command in every hour in all of defined minutes: 0,5,10,...

However, if you use '/', you can write the following simple and short job:

*/5  *  *  *  *  your_command

It means run your_command in the minutes that are dividable by 5 or in the simpler words, '0,5,10,...'

So, dividable symbol '/' is the best choice always;

Mohsen Abasi
  • 2,050
  • 28
  • 30
5

You mention you are using OS X- I have used cronnix in the past. It's not as geeky as editing it yourself, but it helped me learn what the columns are in a jiffy. Just a thought.

Allyn
  • 20,271
  • 16
  • 57
  • 68