ChatGPT解决这个技术问题 Extra ChatGPT

How would I get a cron job to run every 30 minutes?

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.

Have you succeeded in getting your crontab to work? Your problem may not be the crontab, but getting it to be used.
It's running 2 other scripts, one every minute.
I'm waiting to see if one of the answers below works, just have to wait till the next 30 minutes is up...in 3 minutes.
... Couldn't you just change 0,30 to 0,5 ? If 0,5 works, why wouldn't 0,30?
You never told us if it worked on the hour!

T
Tim Cooper

Do:

0,30 * * * * your_command

Works great! In CentOS crontab, I had to do something like 0,30 * * * * root your_command where root would be the user I'm using.
@prograhammer - Was this line part of a root's crontab or some other user's crontab?
@MuhamedHuseinbašić good question, since there are 2 different crontabs (the global one and then a user's). I'll have to check and see...
a
aequalsb

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

https://i.stack.imgur.com/09Nfp.png

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


0 is evenly divisible by 19. So */19 should also run at 1:00, 2:00, etc.
this sounds logical somewhat... i am setting up a cronjob to test
@user34814 you are correct and my answer has been edited... thanks!
E
Eddie

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.


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: 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
Your answer also worked, but Vlad has fewer rep, so I gave him the points.
@DarrylHein lol, Today Eddie has fewer reps :-p . I upvote Eddie.
P
PCheese

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.


M
Mohsen Abasi

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;


A
Allyn

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.


This could be helpful otherwise as well.