Benches macOS

7

Use Launchd to run shell script at boot

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

Hello

I'm trying to create a plist file that will run a shell script at bootup of the server.

I have the following xml so far in the plist file:

<?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>Disabled</key>

<false/>

<key>Label</key>

<string>com.apple.mymount</string>

<key>ProgramArguments</key>

<array>

<string>/mount-script/./mount.sh</string>

</array>

<key>RunAtLoad</key>

<true/>

</dict>

</plist>

The shell script is just a mount command:

#!bin/sh

#MOUNT folder X ON server Y

sudo -u _www mount_smbfs -f 777 -d 777 //username:password@server/folder /mnt/mount

The shell script works fine from the terminal.

I can see the plist has loaded after bootup but the script doesn't run.

What am I doing wrong?

Would need some help please!

7 replies

Sign in or join Beachball Help to reply.

  1. 0

    mschillo

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    hi,

    I can just can't get me head to accept these launchd-plists, I use the free application "Lingon" to let it create these plists. I use it on our live-server and it never failed me.

    cheers,

    Michael.

  2. 0

    Camelot

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    First problem:

    <string>com.apple.mymount</string>

    com.apple is (or at least should be) reserved for Apple scripts, not your own. This should be renamed to your own domain (or your own name if you don't have a domain).

    In addition the .plist file name should match (so if you rename the script Label to be 'com.my.mount' then the .plist should be com.my.mount.plist. You don't include the file name in your post to know if you're already doing that.

    Second:

    <string>/mount-script/./mount.sh</string>

    What? That looks wrong to me. Of course, it
    could be right, but the '/./ in the middle worries me, so I just can't think it is.

    Where is the script, really?

    Third:

    sudo -u _www mount_smbfs -f 777 -d 777 //username:password@server/folder /mnt/mount

    No. Never use 'sudo' within a launchd task. If you need the task to run as a different user then use the
    launchd keyword
    UserName to specify that username:

    <key>UserName</key>

    <string>_www</string>

    I can see the plist has loaded after bootup but the script doesn't run.

    If none of the above fixes it then look at the logs and tell us what it has to say. /var/log/system.log

  3. 0

    MrHoffman

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Lingon: "As of 2009-07-06 0:00:00 GMT, this project is no longer under active development."

  4. 0

    foilpan

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    camelot… you took the words outta my mind.

    Brigante, get rid of the sudo, run the script as the appropriate user, and specify the proper path to the script. those items (at least) will need changing before your launchd task will work. also, be sure the plist is in the proper launchd folder (daemons vs. agents, etc.) and that its permissions are set appropriately.

  5. 0

    Brigante

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Thanks so much for the replies all. I'll go through and make the changes now and let you know how I get on!

    This is the first time I'm using a Mac so I'm still very much learning so please excuse obvious mistakes.

  6. 0

    Brigante

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Made some progress thanks to your help.

    Here's the current version of the plist file:

    <?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple$

    <plist version="1.0">

    <dict>

    <key>Label</key>

    <string>com.my.hnmount</string>

    <key>Disabled</key>

    <false/>

    <key>ProgramArguments</key>

    <array>

    <string>./mount-script/mount</string>

    </array>

    <key>RunAtLoad</key>

    <true/>

    <key>UserName</key>

    <string>_www</string>

    </dict>

    </plist>

    The plist now works when I launch it manually with launchctl load, the folder mounts as the correct user. During boot however it doesn't work.

    I checked the log and there's the following entry:

    Mar 15 15:57:05 lon-ux-web01 com.my.hnmount[73]: mount_smbfs: server connection failed: No route to host

    Mar 15 15:57:05 lon-ux-web01 com.apple.launchd[1] (com.my.hnmount[73]): Exited with exit code: 68

    It seems like the plist runs too early so the network connection has not yet been established hence the connection error message.

    Any idea what I can do to prevent this? Is there some way to make the plist run after a network connection has been established?

    Any ideas?

  7. 0

    Brigante

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Sorted!!

    Worked it out from the launchd man page.

    Here's the finished plist.

    <?xml version="1.0" encoding="UTF-8"?>

    <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple$

    <plist version="1.0">

    <dict>

    <key>Label</key>

    <string>com.my.hnmount</string>

    <key>Disabled</key>

    <false/>

    <key>KeepAlive</key>

    <dict>

    <key>SuccessfulExit</key>

    <false/>

    </dict>

    <key>ProgramArguments</key>

    <array>

    <string>./mount-script/mount</string>

    </array>

    <key>RunAtLoad</key>

    <true/>

    <key>UserName</key>

    <string>_www</string>

    </dict>

    </plist>

    The KeepAlive key was what was needed. Now it keeps trying until the command end with a clean exit. Or at least I think that's what it's doing.

    At least it's working now!

    Thanks again for the help all!