launchd [ALL users]
- Place your script, e.g.,
Test.sh
, in a SHARED location, e.g.,/Users/Shared
- Create a file with extension
.plist
in/Library/LaunchAgents
(requires admin privileges), e.g.,/Library/LaunchAgents/LoginScripts.Test.plist
, by running the following inTerminal.app
:
sudo touch /Library/LaunchAgents/LoginScripts.Test.plist
- Open the file and save it with the following content (make sure your text editor prompts for admin privileges on demand; alternatively, use
sudo nano /Library/LaunchAgents/LoginScripts.Test.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>
<!-- YOUR SELF-CHOSEN LABEL (TASK ID) HERE -->
<string>LoginScripts.Test.sh</string>
<key>ProgramArguments</key>
<array>
<!-- YOUR LITERAL SCRIPT PATH HERE -->
<string>/Users/Shared/Test.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
* The <!-- ... -->
comments indicate the places to customize; you're free to choose a label, but it should be unique - ditto for the .plist
filename; for simplicity, keep the label and the filename root the same.
- From
Terminal.app
, run the following:
sudo chown root /Library/LaunchAgents/LoginScripts.Test.plist
sudo launchctl load /Library/LaunchAgents/LoginScripts.Test.plist
- Note that, as a side effect, the script will execute right away. From that point on, the script will execute whenever ANY user logs on.
- It is not strictly necessary to run
launchctl load
-- since, by virtue of the file's location, it will be picked up automatically on next login -- but it's helpful for verifying that the file loads correctly.