Period scripts

Notes

  1. Bash script is used in the install/uninstall AppleScripts and embedded within the plist file.
  2. Although testing may take longer it is not too difficult to embed Bash within plist files.
  3. & has to be encoded as & and > as > - undo this for testing in the Terminal.
  4. The normal Bash 'pwd' (Present Working Directory) does not work well within AppleScripts.
  5. AppleScript equivalents of 'pwd' are poorly documented but the following is reliable, compact and UNIX-like.

install.app

tell application "System Events" to set pwd to POSIX path of container of (path to me)
do shell script "launchctl unload -w /System/Library/LaunchDaemons/com.apple.periodic-daily.plist;
	launchctl unload -w /System/Library/LaunchDaemons/com.apple.periodic-weekly.plist;
	launchctl unload -w /System/Library/LaunchDaemons/com.apple.periodic-monthly.plist;
	launchctl unload -w /Library/LaunchDaemons/period.plist;
	cp -f " & quoted form of pwd & "/period.plist /Library/LaunchDaemons;
	launchctl load -w /Library/LaunchDaemons/period.plist" with administrator privileges

uninstall.app

do shell script "launchctl load -w /System/Library/LaunchDaemons/com.apple.periodic-daily.plist;
	launchctl load -w /System/Library/LaunchDaemons/com.apple.periodic-weekly.plist;
	launchctl load -w /System/Library/LaunchDaemons/com.apple.periodic-monthly.plist;
	launchctl unload -w /Library/LaunchDaemons/period.plist;
	rm /Library/LaunchDaemons/period.plist" with administrator privileges

period.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" 
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>period</string>
	<key>LowPriorityIO</key>
	<true/>
	<key>Nice</key>
	<integer>1</integer>
	<key>ProgramArguments</key>
	<array>
		<string>/bin/bash</string>
		<string>-c</string>
		<string>d=daily; w=weekly; m=monthly; s=`date +%s`; 
		r(){ g=/var/log/$1.out; test $s -lt `tail -1 $g` || \
		{ /usr/sbin/periodic $1 && echo $[$s + $2] \
		>> $g; }; }; r $d 84600; r $w 603000; r $m 2590200
		</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
	<key>StartInterval</key>
	<integer>3600</integer>
</dict>
</plist>

html | css | 928 hits since April 2012 | updated 2020-06-07 | webmaster