目录

mac制作开机启动的程序

目录
  1. 创建一个 plist 文件,用于描述该服务。在终端中执行以下命令创建一个空文件:

bash

sudo vim /Library/LaunchDaemons/com.example.syncscript.plist   
  1. 在该文件中输入以下内容(注意将 YOUR_USER_NAME 替换为你的实际用户名):

xml

   <?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>
  1. 加载服务:

bash

sudo launchctl load /Library/LaunchDaemons/com.example.syncscript.plist
  1. 查看服务是否被加载

bash

sudo launchctl list | grep syncscript
  1. 启动和重启

bash

sudo launchctl start com.example.syncscript
sudo launchctl stop com.example.syncscript