launchd [ALL users]
- Place your script, e.g.,
Test.sh, in a SHARED location, e.g.,/Users/Shared - Create a file with extension
.plistin/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.
MacOSX后台进程配置
本文介绍了在MacOSX中配置后台进程的方法,包括使用Login Items、XPC Services、Launch Daemons和Launch Agents等不同类型的后台进程。文章还详细解释了如何通过创建.plist文件和shell脚本来实现特定的应用程序在登录时自动运行。
275

被折叠的 条评论
为什么被折叠?



