简体   繁体   中英

OSX launchd with su

I have the following script I want to run every hour. I've set up the launchd to run every hour, on the following simple bash script:

sudo perl /Library/WebServer/CGI-Executables/awstats.pl -config=alpha -update

It doesn't run with sudo nor without sudo. I need sudo for the script to work. Not really sure how to get around this.

Any advice appreciated.

Cron jobs in /etc/crontab can run as root, and thus they don't need sudo to run, you just need sudo to edit the file initially.

Try adding your job to /etc/crontab , and if it doesn't work, provide the syntax you used to run it, and the output from attempting to run it from there, showing why it doesn't work.

sudo runs a command as root; Launch Daemons run as root by default, so sudo is unnecessary and irrelevant. If the script is not running as a Launch Daemon, something else is wrong. Check /var/log/system.log for any errors launchd encountered trying to run the script, and maybe capture the script's output by adding something like this to the .plist file:

<key>StandardOutPath</key>
<string>/var/root/awstats.out</string>
<key>StandardErrorPath</key>
<string>/var/root/awstats.err</string>

Also, make sure you're running the script properly. A launchd .plist file doesn't parse commands like a shell command line, it expects you to hand it pre-parsed elements eg each command argument as a separate string:

<key>ProgramArguments</key>
    <array>
        <string>/usr/bin/perl</string>
        <string>/Library/WebServer/CGI-Executables/awstats.pl</string>
        <string>-config=alpha</string>
        <string>-update</string>
    </array>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM