Search code examples
macosperllaunchd

perl script run from inside launchd failing with "No such file or directory" for 'system' commands


I've got a perl script (which is actually run successfully on Linux & Windows as well) that I'm trying to run via launchd on OSX-10.8.3. I'm fairly inexperienced in writing plist files, so I'm hoping this is something silly that I've overlooked. I should note that it runs fine via cron on OSX, so the problems that I'm encountering are specific to launchd. However, when I attempt to run it via launchd, its erroring out on some perl 'system' calls, claiming "No such file or directory". Here's the plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>Label</key>
        <string>launched.sysinv</string>
        <key>ProgramArguments</key>
        <array>
                <string>/usr/bin/perl</string>
                <string>/Users/lfriedman/cuda-stuff/sw/gpgpu/build/scripts/testing/sysinv.pl</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>StartCalendarInterval</key>
        <dict>
                <key>Hour</key>
                <integer>14</integer>
                <key>Minute</key>
                <integer>30</integer>
        </dict>
        <key>UserName</key>
        <string>lfriedman</string>
        <key>AbandonProcessGroup</key>
        <true/>
        <key>StandardOutPath</key>
        <string>/tmp/sysinv.out</string>
        <key>StandardErrorPath</key>
        <string>/tmp/sysinv.err</string>
</dict>
</plist>

When I load the script with "launchctl load -w launchd.sysinv.plist", I see the following error in /var/log/system.log:

com.apple.launchd[1] (launched.sysinv[51676]): Exited with code: 1

/tmp/sysinv.out has the following output with each invocation:

system(/usr/sbin/system_profiler -detailLevel full &> system_profiler.txt) failed: No such file or directory

/tmp/sysinv.err has a single error (with each invocation):

sh: system_profiler.txt: Permission denied

I'm confused what is causing those errors. Again, running the perl script manually (as the 'lfriedman' user) or via a cronjob (owned by 'lfriedman') works fine, with no errors.


Solution

  • When you run the script via plist, the system call is writing its output (system_profiler.txt) to a directory it does not have permissions to write to. When you run it manually or cron, it writes to a directory that has the appropriate permissions.

    Edit the script to point the output to a directory you have permissions for, e.g. /usr/sbin/system_profiler -detailLevel full &> /tmp/system_profiler.txt