mac制作开机启动的程序
目录
- 创建一个 plist 文件,用于描述该服务。在终端中执行以下命令创建一个空文件:
sudo vim /Library/LaunchDaemons/com.example.syncscript.plist
- 在该文件中输入以下内容(注意将
YOUR_USER_NAME
替换为你的实际用户名):
<?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>com.example.syncscript</string>
<key>ProgramArguments</key>
<array>
<string>/bin/bash</string>
<string>/Users/YOUR_USER_NAME/sync.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StandardOutPath</key>
<string>/Users/YOUR_USER_NAME/Library/Logs/sync.log</string>
<key>StandardErrorPath</key>
<string>/Users/YOUR_USER_NAME/Library/Logs/sync.error.log</string>
</dict>
</plist>
- 加载服务:
sudo launchctl load /Library/LaunchDaemons/com.example.syncscript.plist
- 查看服务是否被加载
sudo launchctl list | grep syncscript
- 启动和重启
sudo launchctl start com.example.syncscript
sudo launchctl stop com.example.syncscript