Search code examples
macosplistlaunchd

How can I avoid using a bash script in my launchd plist?


This is my launchd 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>KeepAlive</key>
  <true/>
  <key>Label</key>
  <string>com.localhost.hexo</string>
  <key>ProgramArguments</key>
  <array>
    <string>/Users/frankg/dev/bin/start-hexo.sh</string>
  </array>
  <key>UserName</key>
  <string>frankg</string>
  <key>RunAtLoad</key>
  <true/>
  <key>StandardErrorPath</key>
  <string>/Users/frankg/hexo</string>
  <key>StandardOutPath</key>
  <string>/Users/frankg/hexo</string>
</dict>
</plist>

This is my bash script

hexo server --cwd /Users/frankg/dev/code/apps/blog  >> /tmp/MyLaunchdTest.out

This is working. How can I avoid the use of a bash script.


Solution

  • Specify the full path to the hexo executable, followed by each argument to it as a separate string in the ProgramArguments array. I'm a bit confused by the various output redirects -- in the current .plist you specify output to /Users/frankg/hexo, but in the script you redirect it to /tmp/MyLaunchdTest.out instead; I assume the latter is what you actually want? AIUI you could also use the WorkingDirectory key to replace the --cwd option. Something like this:

    <?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>KeepAlive</key>
      <true/>
      <key>Label</key>
      <string>com.localhost.hexo</string>
      <key>ProgramArguments</key>
      <array>
        <string>/path/to/hexo</string>
        <string>server</string>
      </array>
      <key>UserName</key>
      <string>frankg</string>
      <key>RunAtLoad</key>
      <true/>
      <key>StandardErrorPath</key>
      <string>/Users/frankg/hexo</string>
      <key>StandardOutPath</key>
      <string>/tmp/MyLaunchdTest.out</string>
      <key>WorkingDirectory</key>
      <string>/Users/frankg/dev/code/apps/blog</string>
    </dict>
    </plist>