Why won’t the LaunchDaemon start

launchdterminalunix

I'm attempting to write my first LaunchDaemon: I think it's pretty simple and meets all the requirements, but it won't run.

The file is at /Library/LaunchDaemons/com.noah.supertest.plist

Ideally, it should be running ls and writing the output to ~/test.txt. But nothing is ever written to the file.

I restarted the machine thinking that might do it, but nothing.

<?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.noah.supertest</string>

  <key>ProgramArguments</key>
  <array>
    <string>ls</string>
    <string>></string>
    <string>~/test.txt</string>
  </array>

  <key>KeepAlive</key>
  <true/>

  <key>StartInterval</key>
  <integer>10</integer>


  <key>RunAtLoad</key>
  <true/>

</dict>
</plist>

Permissions are set as root:wheel

Best Answer

You made several errors:

  • It is running provided you started the plist properly with launchctl - but it's faulty
  • You didn't define a working directory for ls
  • You are missing a proper stdout
  • You are missing a standard error file (then it's usually easy to detect what's wrong with your agent
  • Put it in LaunchAgents instead of LaunchDaemons: ls is no daemon

Here is a working 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>EnableGlobbing</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>com.noah.supertest</string>
    <key>ProgramArguments</key>
    <array>
        <string>ls</string>
        <string>-laO</string>
        <string>/private/tmp</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/tmp/com.noah.supertest.stderr</string>
    <key>StandardOutPath</key>
    <string>/Users/user_name/test.txt</string>
    <key>StartInterval</key>
    <integer>10</integer>
</dict>
</plist>

You have to create the file ~/test.txt first before running the launch agent.

Then start the launch agent with sudo launchctl [subcommand [arguments ...]] and check the result:

...
drwxrwxrwt  8 root      wheel  -       272 Jan 23 12:23 .
drwxr-xr-x@ 6 root      wheel  hidden  204 Apr  9  2015 ..
-rw-r--r--@ 1 username  wheel  -      6148 Jan 23 12:21 .DS_Store
drwx------  3 root      wheel  -       102 Jan 23 12:07 KSOutOfProcessFetcher.0.sAglCyxY5lzPoNgfmEvv-ZqGl-w=
drwx------  3 username  wheel  -       102 Jan 23 12:05 com.apple.launchd.XFA6PYyYos
drwx------  3 username  wheel  -       102 Jan 23 12:05 com.apple.launchd.pHTdYNvPM9
-rw-r--r--  1 username  wheel  -      1867 Jan 23 12:21 com.noah.supertest.stderr
-rw-r--r--  1 username  wheel  -         0 Jan 23 12:16 com.soma-zone.LaunchControl.dumpstate
total 24
drwxrwxrwt  8 root      wheel  -       272 Jan 23 12:23 .
drwxr-xr-x@ 6 root      wheel  hidden  204 Apr  9  2015 ..
-rw-r--r--@ 1 username  wheel  -      6148 Jan 23 12:21 .DS_Store
drwx------  3 root      wheel  -       102 Jan 23 12:07 KSOutOfProcessFetcher.0.sAglCyxY5lzPoNgfmEvv-ZqGl-w=
drwx------  3 username  wheel  -       102 Jan 23 12:05 com.apple.launchd.XFA6PYyYos
drwx------  3 username  wheel  -       102 Jan 23 12:05 com.apple.launchd.pHTdYNvPM9
-rw-r--r--  1 username  wheel  -      1867 Jan 23 12:21 com.noah.supertest.stderr
-rw-r--r--  1 username  wheel  -         0 Jan 23 12:16 com.soma-zone.LaunchControl.dumpstate
total 24
drwxrwxrwt  8 root      wheel  -       272 Jan 23 12:23 .
drwxr-xr-x@ 6 root      wheel  hidden  204 Apr  9  2015 ..
-rw-r--r--@ 1 username  wheel  -      6148 Jan 23 12:21 .DS_Store
drwx------  3 root      wheel  -       102 Jan 23 12:07 KSOutOfProcessFetcher.0.sAglCyxY5lzPoNgfmEvv-ZqGl-w=
drwx------  3 username  wheel  -       102 Jan 23 12:05 com.apple.launchd.XFA6PYyYos
drwx------  3 username  wheel  -       102 Jan 23 12:05 com.apple.launchd.pHTdYNvPM9
-rw-r--r--  1 username  wheel  -      1867 Jan 23 12:21 com.noah.supertest.stderr
-rw-r--r--  1 username  wheel  -         0 Jan 23 12:16 com.soma-zone.LaunchControl.dumpstate
...