Run launchd script at login (macOS)

Veröffentlicht von philipp am

First of all create your bash script and place it somewhere.

Next think of a name in the following reverse domain notation com.mycompanyname.mydepartment.mytaskname.plist and create the property list file (.plist) with the following content:

<?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>at.surworks.backupall</string>
	<key>ProgramArguments</key>
	<array>
		<string>/Users/philipp/Documents/Backups/SURSERV/backup_all.sh</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
	<key>StartInterval</key>
	<integer>3600</integer>
</dict>
</plist>

Now place com.mycompanyname.mydepartment.mytaskname.plist in ~/Library/LaunchAgents and run in terminal:
launchctl load ~/Library/LaunchAgents/com.mycompanyname.mydepartment.mytaskname.plist

This tells launchd to load the task and will run it immediately.

To remove the task from launchd run:

launchctl unload ~/Library/LaunchAgents/com.mycompanyname.mydepartment.mytaskname.plist

You can check if it is loaded with the command by entering:
launchctl list | grep com.mycompanyname.mydepartment.mytaskname

Install Sleepwatcher and fswatch:

brew install sleepwatcher

brew install fswatch

If your script does not run after entering the commands above:

  • complement any executable name by its full path. E.g. I had issues running fswatch, so I had to replace fswatch by /usr/local/bin/fswatch
  • Also it could be that launchd closes the tasks you started in your script when it reaches its end. Then you just have to add following code at the end of your script:
    while : ; do
    sleep 1d
    done

Run launchctl every hour: https://www.splinter.com.au/using-launchd-to-run-a-script-every-5-mins-on/

SLEEPWATCHER: http://www.bernhard-baehr.de/

https://alvinalexander.com/mac-os-x/mac-osx-startup-crontab-launchd-jobs

Sources:

http://www.launchd.info/

https://www.splinter.com.au/using-launchd-to-run-a-script-every-5-mins-on/

https://developer.apple.com/library/content/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html

Kategorien: macOS

0 Kommentare

Schreibe einen Kommentar

Avatar-Platzhalter

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert