Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

9valkyrie

macrumors member
Original poster
Feb 13, 2024
47
17
Back in the glory days of cron, I was able to execute a command every 5 minutes, indefinitely, but also I could turn it off whenever I wanted. cron was super easy to use. Then Apple decided cron was bad idea and replaced it with something super complicated and ridiculous. Does anyone know how execute a bash script every 5 minutes in the new macOS? I'm trying to backup office data to Git on my NAS. My office suite allows the ability to create Flat ODF (git friendly) for everything. And the office app does have autosave, so I just capture everything, commit and push to Git. The office app has poor version control, so I'm making my own. It's worked in the past.
 
Last edited:

chown33

Moderator
Staff member
Aug 9, 2009
10,990
8,874
A sea of green
A launchd Launch Agent should work.

Also see:

What OS version are you running? You may need to grant full disk access to Terminal in order to get things off the ground.

The tool 'plistbuddy' can be used to make the plist. There are other tools for making plists, I think that's just the default one.
 
  • Like
Reactions: 9valkyrie

casperes1996

macrumors 604
Jan 26, 2014
7,593
5,764
Horsens, Denmark
Just to be clear, cron still works and is still there. Here's it's man page in Sonoma:

1723297192577.png
 

9valkyrie

macrumors member
Original poster
Feb 13, 2024
47
17
Just to be clear, cron still works and is still there.

That's very interesting. I was told in recent versions of macOS we should avoid crontab entirely. Many of the CLI utilities are decades old carried over from the original *nix base. Some utilities Apple may not realize that they are still there, lol. Oh, well, it's still fun to learn something new.
 
  • Like
Reactions: casperes1996

casperes1996

macrumors 604
Jan 26, 2014
7,593
5,764
Horsens, Denmark
That's very interesting. I was told in recent versions of macOS we should avoid crontab entirely. Many of the CLI utilities are decades old carried over from the original *nix base. Some utilities Apple may not realize that they are still there, lol. Oh, well, it's still fun to learn something new.
Apple has explicitly integrated cron with their own launchd system so it's very much supported by Apple. Apple does recommend using launchd over cron but they do support using cron managed by launchd
 

9valkyrie

macrumors member
Original poster
Feb 13, 2024
47
17
I may end up going back to cron then. This plist calls my script inconsistently. Somewhere between 3-7 minutes, at random and different times it calls my script. cron is always every 5 minutes consistently.
 

NoBoMac

Moderator
Staff member
Jul 1, 2014
6,244
4,931
Somewhere between 3-7 minutes, at random and different times it calls my script.

I have noticed this as well and have not really done much with launchctl, aside from one "at login" script, due to this.

Where I've seen this is with the periodic command that ships with MacOS. Apple has setup some periodic routines that are supposed to run daily, weekly, monthly. Daily pretty much runs every day, others, not so much.

Last runs of weekly were July 29, Aug 3 & 8. Pretty consistent every 5 days vs 7. Monthly, every couple-ish weeks (Jul 1 & 17, Aug 3).

Now, to be fair, in the launchctl documentation, they do have a bunch of verbiage around "might not run exactly at that time, will be re-scheduled to run if missed the window" mumbo-jumbo, but would think it would cause that big of a swing/error. But, yeah, does not seem reliable if need things to be on a strict schedule.
 

9valkyrie

macrumors member
Original poster
Feb 13, 2024
47
17
I discovered by logs that my script was called every few seconds. I did some research and found out that my plist config was wrong. It now does every 5 minutes. Thanks to everyone's help!
 

Thistle41

macrumors member
Mar 25, 2021
74
39
UK
Just to be clear, cron still works and is still there. Here's it's man page in Sonoma:

View attachment 2404583
Hi, I'm experiencing the same problems (Mac Mini m1 & MBA m1 Sequoia 15.0.1) and previously, several updates back, had a cron setting to do something every 5 minutes and then dump the results into a csv file for analysis. Since Ventura and despite changing the permissions of the .sh file and trying to run it manually - which works btw - I'm wondering if I'm missing something obvious? Cron simply does not pick it up. I've seen that there are 'launch constraints' since the last two updates of the OS, maybe I'm running into that barrier?

I've had a go at constructing a .plist file and putting that in the correct place but got stuck running the launch command. Just seems a lot of effort to do a simple scheduled task that I can set and forget without purchasing specialist tools that I will use just once.

Any alternative way I could use, please?

Thanks
 

casperes1996

macrumors 604
Jan 26, 2014
7,593
5,764
Horsens, Denmark
Hi, I'm experiencing the same problems (Mac Mini m1 & MBA m1 Sequoia 15.0.1) and previously, several updates back, had a cron setting to do something every 5 minutes and then dump the results into a csv file for analysis. Since Ventura and despite changing the permissions of the .sh file and trying to run it manually - which works btw - I'm wondering if I'm missing something obvious? Cron simply does not pick it up. I've seen that there are 'launch constraints' since the last two updates of the OS, maybe I'm running into that barrier?

I've had a go at constructing a .plist file and putting that in the correct place but got stuck running the launch command. Just seems a lot of effort to do a simple scheduled task that I can set and forget without purchasing specialist tools that I will use just once.

Any alternative way I could use, please?

Thanks
So you edit your crontab with crontab -e? - I just tested on Sequoia and adding
* * * * * echo "Test" >> /Users/cs/test.txt
to my crontab through crontab -e, I do indeed get "Test" appended to that file every minute; I have done nothing special to this to my knowledge but I am using my Mac for development, so things like having my Terminal have Full Disk Access and having Xcode command line tools installed is not something I've tested without
 
  • Like
Reactions: Thistle41

Thistle41

macrumors member
Mar 25, 2021
74
39
UK
So you edit your crontab with crontab -e? - I just tested on Sequoia and adding
* * * * * echo "Test" >> /Users/cs/test.txt
to my crontab through crontab -e, I do indeed get "Test" appended to that file every minute; I have done nothing special to this to my knowledge but I am using my Mac for development, so things like having my Terminal have Full Disk Access and having Xcode command line tools installed is not something I've tested without
Thanks for that. Yes I edit with crontab -e no problem. I will try that one-line test thank you for the idea.

It could be that I have no Xcode installed, deliberately due to the massive disk consumption (I have the base model so...). Yes I've given full disk access to Terminal as well.

I've rigged something up using Automator and attached to Calendar to run a shell script that calls *.sh files once a day with a loop count of 288 for every 5 min/day and the Automator job gets called each day.
 

casperes1996

macrumors 604
Jan 26, 2014
7,593
5,764
Horsens, Denmark
Hi, I'm experiencing the same problems (Mac Mini m1 & MBA m1 Sequoia 15.0.1) and previously, several updates back, had a cron setting to do something every 5 minutes and then dump the results into a csv file for analysis. Since Ventura and despite changing the permissions of the .sh file and trying to run it manually - which works btw - I'm wondering if I'm missing something obvious? Cron simply does not pick it up. I've seen that there are 'launch constraints' since the last two updates of the OS, maybe I'm running into that barrier?

I've had a go at constructing a .plist file and putting that in the correct place but got stuck running the launch command. Just seems a lot of effort to do a simple scheduled task that I can set and forget without purchasing specialist tools that I will use just once.

Any alternative way I could use, please?

Thanks
If you still have issues, I have a cron-job debugging tool on my GitHub I call cronx - It's pretty simple, it just executes a crontab command here and now so you can check it works without having to wait for it
Compatible with both macOS and Linux - at least last I touched it :p
 
  • Like
Reactions: Thistle41

Thistle41

macrumors member
Mar 25, 2021
74
39
UK
If you still have issues, I have a cron-job debugging tool on my GitHub I call cronx - It's pretty simple, it just executes a crontab command here and now so you can check it works without having to wait for it
Compatible with both macOS and Linux - at least last I touched it :p
Thanks, the cron example you gave does not run so I'll give this debugging tool a go.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.