Mac launchd help

Latch

macrumors newbie
Original poster
Sep 27, 2012
2
0
Hello, I am new to these forums, so I hope I don't do anything too stupid.

I'll just give all the details of what I would like to do.

I would like to run a shell script at 8:00AM every Monday, Wednesday, and Friday.

What this shell script does is run a python script that automatically downloads the newest xkcd comic.

If there is a more efficient way to do this, please let me know, but my primary concern is how to use launchd to set up this script scheduling.

Thanks in advance!
 

kryten2

macrumors 6502a
Mar 17, 2012
996
59
Belgium
You can execute a shell script from Applescript with the do shell script command. You can run an Applescript from iCal as an alarm.

More info on launchctl and launchd.plist here :

https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man1/launchctl.1.html#//apple_ref/doc/man/1/launchctl
https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html#//apple_ref/doc/man/5/launchd.plist

or in Terminal :

Code:
man launchctl
man launchd.plist

StartCalendarInterval <dictionary of integers or array of dictionary of integers>
This optional key causes the job to be started every calendar interval as specified. Missing arguments
are considered to be wildcard. The semantics are much like crontab(5). Unlike cron which skips job
invocations when the computer is asleep, launchd will start the job the next time the computer wakes
up. If multiple intervals transpire before the computer is woken, those events will be coalesced into
one event upon wake from sleep.

Minute <integer>
The minute on which this job will be run.

Hour <integer>
The hour on which this job will be run.

Day <integer>
The day on which this job will be run.

Weekday <integer>
The weekday on which this job will be run (0 and 7 are Sunday).

Month <integer>
The month on which this job will be run.
Can you post your python script? Thanks
 

Latch

macrumors newbie
Original poster
Sep 27, 2012
2
0
The iCal -> applescript -> shell script -> python script is actually what I am using right now, and it works... I was just hoping for a more elegant solution.

I attached a zip file. Unzip it into the folder where you want all of the comics to be downloaded to. It includes the .py file, the applescript, and the shell script. It also contains a file called _count.txt, which is used to store the last comic number the script downloaded. So, if you put a 1 in it, it will download comic 1 and keep going all the way up until the most recent comic. When it gets a 404 error, because it is trying to download a comic that Randall has not made yet, call it comic 1200, it will overwrite the contents of _count.txt, replacing the contents with 1200. Thus, when the script starts again, it will keep going where it last left off. Unless the script gets interrupted in the middle, in which case _count.txt will remain at whatever it was when the script was started.

The reason the files all have _ in front of them is so that they are all grouped together at the beginning of the folder (assuming organized by name). Otherwise, they would get lost in the sea of 1000+ xkcd comics.

Have fun! I hope my code is commented well enough you can sort of tell what it does!
 

Attachments

kryten2

macrumors 6502a
Mar 17, 2012
996
59
Belgium
I made a launchagent you can use. Open it with TextEdit to see if anything regarding paths need to be changed. Copy this to your /Users/yourusername/Library/LaunchAgents folder. Give yourself read and write permissions on the plist file. In Finder-->Get Info and change the Sharing & Permissions at the bottom of the window. Load the plist file with launchctl. In Terminal :

Code:
launchctl load /Users/yourusername/Library/LaunchAgents/nameofplistfile.plist
Tested by downloading the last 5 images.
 

Attachments

Last edited: