简体   繁体   中英

launchd: bash command doesn't run

I'm still a bit shaken after realising OS X doesn't offer straightforward solution to schedule tasks. Maybe that's the reason why I'm not able to find the reason behind my launchd plist issues. I thought that's gonna be an easy task and after a bit of reading I came with:

Frost:~ ren$ cat /Library/LaunchDaemons/org.mvps.hosts.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>Label</key>
    <string>org.mvps.hosts</string>
    <key>ProgramArguments</key>
    <array>
        <string>bash</string>
        <string>-c</string>
        <string>curl -s http://winhelp2002.mvps.org/hosts.txt | grep \"^0\.0\.0\.0\|^127\.0\.0\.1\|^::1\" &gt; /etc/hosts</string>
    </array>
    <key>StartInterval</key>
    <integer>86400</integer>
</dict>
</plist>

but for everything in the world I can't figure why after loading and starting /etc/hosts doesn't update. The command itself works so the issue is probably in formatting or escaping..? I didn't find any way to get any other lead than launchctl list status 127 for the job, whatever it points to. Any ideas would be appreciated.

This is what I end up with:

Frost:~ ren$ cat /Library/LaunchDaemons/org.mvps.hosts.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>Label</key>
    <string>org.mvps.hosts</string>
    <key>ProgramArguments</key>
    <array>
        <string>bash</string>
        <string>-c</string>
        <string>bash -c "curl -s http://winhelp2002.mvps.org/hosts.txt | grep \"^0\.0\.0\.0\|^127\.0\.0\.1\|^::1\" &gt; /etc/hosts"</string>
    </array>
    <key>StartInterval</key>
    <integer>86400</integer>
</dict>
</plist>

Frankly, I have no idea why I have to call bash to call bash to run command, it's something with inner workings on launchd which I don't like anyway. It works now.

Have you tried absolute path for bash and/or curl and/or grep? ie /bin/bash /usr/bin/curl or wherever they stay on your fs?

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