I'm trying to get familiar with cron jobs, and I think I get the basic idea (scheduling, syntax, etc), But, I can't seem to get it right on my mac with Terminal - where exactly do I find the Crontab? How should I reference the paths to scripts?
What I'm trying to do is hit a php script on a remote machine (http://...) - Is that possible at all?
Although launchd is the preferred method in macOS, the cron method still works in macOS as well. cron is a Linux utility that schedules a command or script on your server/computer to run automatically at a specified time and date. A cron job is the scheduled task and it is very useful to automate repetitive tasks.
To see your active cron jobs, you can use the crontab -l command. If you want to, for instance, run a Python script using a cron job, you'll have to deal with some added complexity. The way you should set this up is by having two scripts — the Python script and an executable shell script that runs the Python script.
The cron reads the crontab (cron tables) for running predefined scripts. By using a specific syntax, you can configure a cron job to schedule scripts or other commands to run automatically.
Type crontab -e
to edit your cron table and crontab -l
to list the current contents.. Type man 1 crontab
for more info on that command and man 5 crontab
for more info on the cron table file format.
For example, to download the stackoverflow page every day at 10:00a, run crontab -e
, enter this line, and then save/quit. The output will be written to a file in your home directory.
0 10 * * * /usr/bin/curl -s http://stackoverflow.com > ~/stackoverflow.html
To get started with launchd (instead of cron) you'll want to first create an empty .plist
file, for example local.mytask.plist
and put it somewhere. ~/Library/LaunchAgents
is probably a good place. Open that in text editor and copy in the code below
<?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>KeepAlive</key>
<false/>
<key>Label</key>
<string>local.mytask</string>
<key>ProgramArguments</key>
<array>
<string>/opt/local/bin/wget</string>
<string>http://someserver/somepage.php</string>
</array>
<key>StartInterval</key>
<integer>300</integer>
<key>RunAtLoad</key>
<true />
<key>StandardErrorPath</key>
<string>/dev/null</string>
<key>StandardOutPath</key>
<string>/dev/null</string>
</dict>
</plist>
Then "activate" the file from the command line:
sudo launchctl load /Users/my_username/Library/LaunchAgents/local.mytask.plist
To make it load automatically, create a ~/.launchd.conf
file with the same line (minus sudo launch
)
load /Users/my_username/Library/LaunchAgents/local.mytask.plist
The above instructions above have been copied from www.davidlanier.com and reposted here for your reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With