Launch Agent

psherotov
Contributor

For some specific reasons, I'm trying/testing out creating a Launch Agent to run when users login. The agent has one job--to run a script stored locally on machines.

It is not working :( I wrote it based on examples and guides for plist format I found online. I've seen some GUI tools for creating and editing plists but don't know which, if any, are good.

Here is the content of the 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>com.dockset.app</string>

        <key>ProgramArguments</key>
        <array>
            <string>/Library/Scripts/K-3Dock.sh</string>
        </array>

        <key>RunAtLoad</key>
        <true/>

     <key>StandardErrorPath</key>
  <string>/tmp/Dockset1.err</string>

  <key>StandardOutPath</key>
  <string>/tmp/Dockset1.out</string>   
    </dict>
</plist>

I've checked to make sure the permissions match the other plists stored in /Library/LaunchAgents

I've made sure the permissions on it are 600 and that it's executable.

The plist is named: com.dockset.test.plist

I haven't tried making the name of the plist match the label, not sure if that's the issue though.

I've run plutil on it and it has come up ok.

Any guidance would be greatly appreciated.

6 REPLIES 6

sdagley
Esteemed Contributor II

@psherotov I've never not used the plist name as the Label, but I don't think things don't work if they're different. Have you made sure the K-3Dock.sh script you're calling is executable? The best online reference for LaunchAgents and LaunchDaemons is https://www.launchd.info. It was created by the publishers of LaunchControl which is an excellent tool for creating/modifying LaunchDaemons and LaunchAgents.

One change you might try, if your script is a bash script, is use the following for your ProgramArguments array:

<array>
    <string>/bin/bash</string>
    <string>/Library/Scripts/K-3Dock.sh</string>
</array>

rqomsiya
Contributor III

Permissions should be 644

chmod 644 file_name

and owner should be root:wheel

chown root:wheel file_name

psherotov
Contributor

@sdagley and @rqomsiya Thanks for the input I made the changes/checked/confirmed permissions.

Here's screenshots of plist location and permissions:

c7b77d66f897493c92c79113532d112d
59f025cbd56e4aa2b1ca6d7ecfa3e446
b7fdd99c42d0404a8be9a4669ebe5ad8

Also, here's the script itself:

#!/bin/bash

#We need to wait for the dock to actually start
until [[ $(pgrep Dock) ]]; do
    wait
done

#Get the current logged in user that we'll be modifying
if [ ! -z "$3" ]; then
    user=$3
else
    user=$(python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");')
fi

#Set variables
du="/usr/local/bin/dockutil"
userHome="/Users/$user"
networkHome="smb://server.com/Students$/$user"
thinkinThings="Applications/Thinkin' Things 1/Thinkin' Things 1.app"

#Function for applying dock configuration
createBaseDock()
{
    #Remove all items for logged in user
    $du --remove all --no-restart $userHome

    #Adding base items to the dock    
    $du --add '/Applications/Google Chrome.app' --position 1 --no-restart $userHome
    $du --add '/Applications/Safari.app'  --position 2 --no-restart $userHome
    $du --add '/Applications/Comic Life 3.app' --position 4 --no-restart $userHome
    $du --add '/Applications/Doozla.app' --position 3 --no-restart $userHome
    $du --add '/Applications/FableVision/Stationery Studio/Stationery Studio 1.2.app' --position 5 --no-restart $userHome
    $du --add '/Applications/The Print Shop 2.app' --position 6 --no-restart $userHome
    $du --add "$thinkinThings" --position 7 --no-restart $userHome
}

#Function for finishing base dock
finishBaseDock()
{
    #Add local downloads
    $du --add '~/Downloads' --section others --position last --no-restart $userHome

    killall Dock
}

    case $user in
        p|k)
            echo "p or k found"
            createBaseDock
            finishBaseDock
            ;; 
        1)
            echo "1 found"
            createBaseDock
            $du --add '/Applications/Google Earth.app' --position 8 --no-restart $userHome
            $du --add '/Applications/Photo Booth.app' --position 9 --no-restart $userHome
            finishBaseDock
            ;;
        2)
            echo "2 found"
            createBaseDock
            $du --add '/Applications/Google Earth.app' --position 8 --no-restart $userHome
            $du --add '/Applications/Photo Booth.app' --position 9 --no-restart $userHome
            $du --add '/Applications/Lego Digital Designer.app' --position 10 --no-restart $userHome
            finishBaseDock
            ;;
        3)
            echo "3 found"
            createBaseDock
            $du --add '/Applications/Google Earth.app' --position 8 --no-restart $userHome
            $du --add '/Applications/Photo Booth.app' --position 9 --no-restart $userHome
            $du --add '/Applications/Lego Digital Designer.app' --position 10 --no-restart $userHome
            $du --add '/Applications/Mavis Beacon Teaches Typing.app' --position 11 --no-restart $userHome
            $du --add '/Applications/Adobe Photoshop CS6/Adobe Photoshop CS6.app' --position 12 --no-restart $userHome
            finishBaseDock
            ;;
    esac

exit 0

Still not working :(

Please let me know if you have any additional suggestions or if you spot a goof on my part. :)

psherotov
Contributor

Just a follow-up. Nothing is getting written to the logs specified in the plist.

sdagley
Esteemed Contributor II

@psherotov Your K-3Dock.sh script doesn't have an executable flag. Do a sudo chmod +x /Library/Scripts/K-3Dock.sh to set that.

psherotov
Contributor

@sdagley Thanks. That's the trouble with working on something to much, you start to miss obvious stuff! :)

I also figured out why the agent was failing. Our student accounts don't have access to /tmp so it was failing and not running the script. The revised plist below works fine!

<?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.dockset.app</string>

        <key>ProgramArguments</key>
        <array>
            <string>/Library/Scripts/K-3Dock.sh</string>
        </array>

        <key>RunAtLoad</key>
        <true/>
</dict>
</plist>