Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting started with cronjobs on a Mac

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?

like image 839
Adam Avatar asked Nov 05 '09 21:11

Adam


People also ask

Does cron work on macOS?

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.

How do I see cron jobs on Mac?

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.

Do CronJobs run automatically?

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.


2 Answers

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
like image 60
Ryan Bright Avatar answered Sep 27 '22 18:09

Ryan Bright


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.

like image 37
saschabeaumont Avatar answered Sep 27 '22 19:09

saschabeaumont