0

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

  • Possible duplicate of Run bash script at login stored in the home folder?. – Gordon Davisson Jan 23 '16 at 6:16
  • Interesting, but I removed the ~ and entered the full path with no luck. – nipponese Jan 23 '16 at 6:49
  • what about adding <key>EnableGlobbing</key> <true/> and <key>WorkingDirectory<key><string>/path/to/dir</string> ? – Mateusz Szlosek Jan 23 '16 at 7:39
  • What are the permissions of the folder? – Kent Jan 23 '16 at 7:43
  • @Kent On /Library/LaunchDaemons/ or home? – nipponese Jan 23 '16 at 7:46
2

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
...
| improve this answer | |
  • Not really sure what this LaunchAgent is suppose to do. I ran ls -la0 /private/tmp and output didn't make any sense. – nipponese Jan 23 '16 at 12:03
  • @nipponese it just was an example. you may specify any other directory – klanomath Jan 23 '16 at 12:06
  • @nipponese Which directory do you want to supervise with ls? Or what's your plan at all (with ls)? – klanomath Jan 23 '16 at 12:09
  • Just trying to understand how the ProgramArguments works. Eventually, I just want to run an rsync every 10 seconds. – nipponese Jan 24 '16 at 4:36
  • 1
    @nipponese Probably it's better to just call an executable shell script (containing the complete rsync command) with a launch agent then: link. – klanomath Jan 24 '16 at 9:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .