How do I have a script run every, say 30 minutes? I assume there are different ways for different OSs. I'm using OS X.
7 Answers
Just use launchd. It is a very powerful launcher system and meanwhile it is the standard launcher system for Mac OS X (current OS X version wouldn't even boot without it). For those who are not familiar with launchd
(or with OS X in general), it is like a crossbreed between init
, cron
, at
, SysVinit (init.d
), inetd
, upstart
and systemd
. Borrowing concepts of all these projects, yet also offering things you may not find elsewhere.
Every service/task is a file. The location of the file depends on the questions: "When is this service supposed to run?" and "Which privileges will the service require?"
System tasks go to
/Library/LaunchDaemons/
if they shall run no matter if any user is logged in to the system or not. They will be started with "root" privileges.
If they shall only run if any user is logged in, they go to
/Library/LaunchAgents/
and will be executed with the privileges of the user that just logged in.
If they shall run only if you are logged in, they go to
~/Library/LaunchAgents/
where ~ is your HOME directory. These task will run with your privileges, just as if you had started them yourself by command line or by double clicking a file in Finder.
Note that there also exists /System/Library/LaunchDaemons
and /System/Library/LaunchAgents
, but as usual, everything under /System
is managed by OS X. You shall not place any files there, you shall not change any files there, unless you really know what you are doing. Messing around in the Systems folder can make your system unusable (get it into a state where it will even refuse to boot up again). These are the directories where Apple places the launchd
tasks that get your system up and running during boot, automatically start services as required, perform system maintenance tasks, and so on.
Every launchd
task is a file in PLIST format. It should have reverse domain name notation. E.g. you can name your task
com.example.my-fancy-task.plist
This plist can have various options and settings. Writing one per hand is not for beginners, so you may want to get a tool like LaunchControl (commercial, $18) or Lingon (commercial, $14.99) to create your tasks.
Just as an example, it could look like this
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.example.my-fancy-task</string>
<key>OnDemand</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/bin/sh</string>
<string>/usr/local/bin/my-script.sh</string>
</array>
<key>StartInterval</key>
<integer>1800</integer>
</dict>
</plist>
This agent will run the shell script /usr/local/bin/my-script.sh every 1800 seconds (every 30 minutes). You can also have task run on certain dates/times (basically launchd can do everything cron can do) or you can even disable "OnDemand" causing launchd to keep the process permanently running (if it quits or crashes, launchd will immediately restart it). You can even limit how much resources a process may use.
Update: Even though OnDemand
is still supported, it is deprecated. The new setting is named KeepAlive
, which makes much more sense. It can have a boolean value, in which case it is the exact opposite of OnDemand
(setting it to false
behaves as if OnDemand
is true
and the other way round). The great new feature is, that it can also have a dictionary value instead of a boolean one. If it has a dictionary value, you have a couple of extra options that give you more fine grain control under which circumstances the task shall be kept alive. E.g. it is only kept alive as long as the program terminated with an exit code of zero, only as long as a certain file/directory on disk exists, only if another task is also alive, or only if the network is currently up.
Also you can manually enable/disable tasks via command line:
launchctl <command> <parameter>
command can be load
or unload
, to load a plist or unload it again, in which case parameter is the path to the file. Or command can be start
or stop
, to just start or stop such a task, in which case parameter is the label (com.example.my-fancy-task
). Other commands and options exist as well.
Update: Even though load
, unload
, start
, and stop
do still work, they are legacy now. The new commands are bootstrap
, bootout
, enable
, and disable
with slightly different syntax and options. One big difference is that disable
is persistent, so once a service has been disabled, it will stay disabled, even across reboots until you enable it again. Also you can use kickstart
to run a task immediately, regardless how it has been configured to run.
The main difference between the new and the old commands is that they separate tasks by "domain". The system has domain and so has every user. So equally labeled tasks may exist in different domains and launctl
can still distinguish them. Even different login and different UI sessions of the same user have their own domain (e.g. the same user may once be logged locally and once remote via SSH and different tasks may run for either session) and so does every single running processes. Thus instead of com.example.my-fancy-task
, you now would use system/com.example.my-fancy-task
or user/501/com.example.my-fancy-task
to identify a task, with 501 being the user ID of a specific user.
See documentation of the plist format and of the launchctl
command line tool.
-
1What if I as a user want something to run even when I'm not logged in? Is that possible, or do I have to ask the admin to put something in /Library/LaunchDaemons? Sep 25, 2008 at 16:19
-
@Mark: yes, your admin would have to do this, and the UserName and GroupName keys would have to be set appropriately in the .plist file. Feb 7, 2009 at 22:30
-
2LaunchControl soma-zone.com/LaunchControl is very useful, it has an unlimited trial, as far as i can tell there are no limitations vs the licensed version. Dec 12, 2017 at 15:37
-
1@MaciekRek
StartCalendarInterval
with an array of two dictionaries, both with the keyMinute
and a value of once00
and once30
would do exactly what you've requested. Please ask questions in the future as questions, not as comments, then I can also show you the final dictionary (I can't do that in a comment). This site is all about asking questions. If you want to hint someone at the question, post the link to the question as a comment but not the question itself.– MeckiJul 22, 2020 at 20:08 -
1@Noumenon Yes, they will. Just keep in mind that access to certain keychain items is not possible while the system is in locked state, so your task needs access to anything in keychain, this access may fail. Even if you fast user switch to another user, they will keep running. The only thing that will prevent them from running is your system going to sleep or your user being logged out of the system.– MeckiDec 6, 2021 at 2:06
you could use the very convenient plist generator: http://launched.zerowidth.com/ (no need to buy anything…)
it will give you a shell one-liner to register a new scheduled job with the already recommended launchd
On MacOSX, you have at least the following options:
From personal experience, cron is the most reliable. When I tested, launchd had a number of bugs and quirks. iCal alarms only run when you are logged in (but that might be something you prefer).
Syntax of Cron
You can use cron to schedule tasks.
crontab -e
A job is specified in the following format.
* * * * * command to execute
│ │ │ │ │
│ │ │ │ └─── day of week (0 - 6) (0 to 6 are Sunday to Saturday, or use names; 7 is Sunday, the same as 0)
│ │ │ └──────── month (1 - 12)
│ │ └───────────── day of month (1 - 31)
│ └────────────────── hour (0 - 23)
└─────────────────────── min (0 - 59)
Example:
0 12 * * * cd ~/backupfolder && ./backup.sh
Translation - Run every day at noon
Example:
45 * * * * cd ~/backupfolder && ./backup.sh
Translation - Run once an hour every day at 45 mins
Registering the job
You can run your script as root.
sudo crontab -e
Once you installed your cron tasks, you can use crontab -l to list your tasks.
crontab -l
If you want to know more about cron schedule expressions, you can access
https://crontab.guru https://ole.michelsen.dk/blog/schedule-jobs-with-crontab-on-mac-osx.html
-
1
-
-
While it looked promising using
crontab -e
, it fails at the end requiring admin rights. Suspect it's some linkage that might be a one-off, but currently a dead-end for me.– dszApr 10, 2022 at 23:52
As Mecki pointed out, launchd would be the way to go with this. There's a GUI interface for launchd called Lingon that you might want to check out, as opposed to editing the launchd files by hand:
Lingon is a graphical user interface for creating an editing launchd configuration files for Mac OS X Leopard 10.5.
[snip...]
Editing a configuration file is easier than ever in this version and it has two different modes. Basic Mode which has the most common settings readily available in a very simple interface and Expert Mode where you can add all settings either directly in the text or insert them through a menu.
-
Honestly, this is a great, easy-to-use interface that just gets it done and allows you to move on. If you're short on time, this is the way to go. Nov 13, 2020 at 22:53
MAC OS has an Automator Tool which is same as that of Task Scheduler in windows. And using Automator you can schedule tasks on daily basis and link the task with recurring calendar event to run scripts on specified time daily. refer link to run scripts on daily basis in Mac OS
-
At least in macOS Catalina (10.15.x), I couldn't find a way to schedule tasks in Calendar through Automator. I had much better results with Lingon (see the other answers that mention as an interface to launchd).– calvinfSep 15, 2020 at 0:08
For apple scripts, I set up a special iCal calendar and use alarms to run them periodically. For command line tools, I use launchd.