1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Nightmare with loginhooks

Discussion in 'Mac Programming' started by $hrap, May 7, 2013.

  1. macrumors newbie

    Hi all,

    New to the forum - so I apologies if this query is in the wrong place.

    I work in a secondary school and we have an AD/OD environment. Users authenticate via their AD username/password and OD is used to define any policies via workgroup manager on our XServe running Lion Server.

    We have 25 iMac clients all running Lion - all I'm trying to do is place an alias on the desktop when a pupil logs in which directs them to an SMB share on a Windows 2008 server (<server>\shared docs\garageband>)

    The SMB share is called "garageband" and it allows pupils to drag their garageband work into it for teachers to see and review - pupils can read/write to this share.

    Now, so far I've created the following shell script, called garageband.sh, changed it to executable (chmod +x garageband.sh) and placed it in /Library/Scripts:

    osascript -e 'tell application "Finder"' -e 'make new alias to folder (posix file "/Volumes/GarageBand") at desktop' -e 'end tell'
    This works, providing the smb /volumes/garageband is mapped when authenticated users first logon (this is done via workgroup manager).

    I've then followed various tutorials on how to make loginhooks work on the client: http://krypted.com/mac-os-x/login-logout-hooks/

    However, when I log into the client - nothing happens. It does not create an alias on the desktop.

    I've also tried adding the garageband.sh script to Workgroup Manager - computer group - preferences - login scripts - garageband.sh scripts. However the clients ignore it.

    I'm no pro when it comes to scripting, and what makes it worse is that I didn't setup the OSX server or environment in the first place.

    Any suggestions would be helpful. Thanks guys.
  2. macrumors 6502a

    I'm guessing that the script is executed immediately but before the smb /volumes/garageband is mapped. I've helped another forum member with a Logout hook in another thread here. Maybe there's some useful info for you there. Some things you can try :

    [COLOR="Red"]/usr/bin/[/COLOR]osascript -e 'tell application "Finder"' -e 'make new alias to folder (posix file "/Volumes/GarageBand") at desktop' -e 'end tell'
    Make a new script and check if the volume GarageBand is mounted when you log into the client :

    su - $1 -c "/sbin/mount > ~/Desktop/mount.log"
    Login and logout scripts are a deprecated technology. In most cases, you should use launchd jobs instead. See Login and Logout Scripts and how-to-run-a-script-at-login-logout-in-os-x
  3. macrumors newbie

    Kryten, thanks for your help. I've made some progress.

    I used a programme called Lingon to create a launchagent (com.myscript.garageband.plist) which is placed in /library/launchagents and calls garageband.sh (that you gave me) found in /usr/sbin:

    <?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">

    /usr/bin/osascript -e 'tell application "Finder"' -e 'make new alias to folder (posix file "/Volumes/GarageBand") at desktop' -e 'end tell'
    This works - a pupil logs on, and there is the garageband alias on the desktop.

    I've also made a logouthook script that deletes the garageband alias from the desktop - this works too.

    Now, the problem. The above worked on my iMac, but when I tried it on a macbook pro (running Lion) - it didn't work. I did the following:

    copied the above plist file to \library\launchagents
    copied the garageband.sh script to \usr\sbin

    When I log onto the macbook pro as a pupil - it fails to create the alias.

    Do you know what i'm missing? Do I have to run any sudo commands, or do you think that I may need to have Lingon installed on every client that runs the launch agent perhaps?

  4. kryten2, May 8, 2013
    Last edited: May 8, 2013

    macrumors 6502a

    You definitely do NOT need to have Lingon installed on every client. Lingon just creates the launchd plist files and has nothing to do with it working or not. I'm thinking it 's an ownership problem. Things to check on the macbook pro (running Lion) :

    Check if your com.myscript.garageband.plist launchagent is loaded. Open Terminal application and type the following line and press enter :

    launchctl list | grep myscript
    Example :

    Aldebaran:~ test$ launchctl list | grep down
    -	0	com.test.downloadsmove
    If your com.myscript.garageband.plist launchagent is loaded it should produce some output. If no output is returned it's not loaded.

    Load it manually. Type the folowing line in Terminal and press enter:

    launchctl load /path/to/your/com.myscript.garageband.plist
    If there's an ownership problem launchctl will complain about it.

    Example :

    Aldebaran:~ test$ launchctl load Library/LaunchAgents/com.test.downloadsmove.plist 
    launchctl: Dubious ownership on file (skipping): Library/LaunchAgents/com.test.downloadsmove.plist
    nothing found to load

    Note : Unix is case sensitive!
    /library/launchagents is not the same as /Library/LaunchAgents
    \library\launchagents and \usr\sbin --> forward slashes please!
  5. macrumors 603

    IIRC, launchd plists residing in /Library/LaunchAgents must be owned by root, and have read-only permissions for group and other (i.e. all except owner).

    If these conditions aren't met, then launchd silently ignores the plist.

    This is done for security reasons.
  6. $hrap, May 13, 2013
    Last edited: May 13, 2013

    macrumors newbie

    Kryten - you're right, this is the message I got when I ran launchctl load manually.

    Sorry for late response. We have a network meltdown last week so I had to sideline the launch agent problem. How do I get round the dubious file permission problem? Do I run the chmod 755 command?

    The other problem I've encountered is that when the same pupil logs in multiple times - it recreates the garageband alias - the pupil ends up with multiple copies of the alias on the desktop.

  7. macrumors 6502a

    In Terminal :

    sudo chown 0:0 /path/to/your/com.myscript.garageband.plist
    sudo chmod 644 /path/to/your/com.myscript.garageband.plist
    Multiple alias problem. Try this :

    tell application "Finder"
    	set fileExists to exists file "GarageBand" of desktop
    	if not fileExists then make new alias to folder (POSIX file "/Volumes/GarageBand") at desktop
    end tell
  8. $hrap, May 13, 2013
    Last edited: May 13, 2013

    macrumors newbie


    I've managed to sort the duplicate aliases. I just added the following to a predefined logout.sh script I found buried in Library/Scripts:

    rm ~/Desktop/GarageBand
    However, the chown commands didn't work, I've tried:

    sudo chown 0:0 /library/lauchagents/com.myscript.garageband.plist
    sudo chmod 644 /library/lauchagents/com.myscript.garageband.plist
    Still no luck when a pupil logs on.

    Further info - I have the following:

    garageband.sh - located in /Library/Scripts/Myscripts
    com.myscript.garageband.plist - located in /Library/Launchagents - this called the garageband.sh script

    Do I have to perform the chown command on the garageband.sh script, too?
  9. kryten2, May 13, 2013
    Last edited: May 13, 2013

    macrumors 6502a

    It confuses me a bit that you keep mixing lowercase and uppercase examples to paths in your replies. Please post the output of the following commands in Terminal. Post it as a screenshot of your Terminal window so it's clear exactly what you typed in and what output is returned from the commands.

    Log on as pupil on that Lion machine :

    ls -al /Library/Scripts/Myscripts/garageband.sh
    ls -al /Library/LaunchAgents/com.myscript.garageband.plist
    sudo chown -v 0:0 /Library/LaunchAgents/com.myscript.garageband.plist
    sudo chmod -vv 644 /Library/LaunchAgents/com.myscript.garageband.plist
    (may need sudo as well)launchctl list | grep myscript
    # This is a comment. No need to type this. If the launchctl list shows no output load the com.myscript.garageband.plist manually
    (may need sudo as well)launchctl load /Library/LaunchAgents/com.myscript.garageband.plist
    As a pupil on that Lion machine :

    run the garageband.sh shell script manually if execution is allowed.

    You might also want to check some logs in the Console application.
  10. macrumors newbie

    The lower and upper case mixture is not intentional, to be honest I've not even noticed it until you pointed it out. I apologise if it's confused you, though.

    I'll run the command tomorrow when I get to work.

  11. macrumors newbie


    After a log day battling with the Garageband alias, I've finally made some progress.

    The reason why the LaunchAgent wasn't running for pupils is because "system" did not have read/write access in the permissions of the .plist file in Library/LaunchAgents.

    What I did was recreate the .plist using Lingon, then I copied it with it's current permissions to another mac (along with the garageband.sh script) and it worked.

    However, I've facing a new problem:

    When a pupil launches Garageband, and they try to save a new project to the GarageBand alias folder - it lets them into the folder but it will not allow them to save to it, OSX 10.7 complains that the user doesn't have permission to save to the folder - yet they can get to it and save correctly via the mounted smb share volume - smb:\\Shared Documents\GarageBand.

    GarageBand is a folder on a Windows Server it has the correct permissions and is set to allow pupils to write to the folder.

    Is there something I have to put into the garageband.sh script to set permissions for the alias to allow pupils to read/write to the shortcut?

    I'm getting multiple permission errors "The operation can't be completed because you don't have permission to access some of the items" is one of them.
    Thanks for your help so far.
  12. macrumors newbie


    Abandoned the alias script. I've decided to try a symbolic link instead.

    I pointed my LaunchAgent (Library/LaunchAgents/com.MyScript.GarageBand.plist) to /Library/Scripts/MyScripts/sym-link.sh, which contains the following:

    ln -s "/Volumes/RMShared Documents/GarageBand" ~/Music/GarageBand
    When I log on as a pupil, sure enough, the default Garageband folder \Users\Music\Garageband links itself to the mounted SMB share smb://<server>/SharedDocuments/Garageband, which is what I want it to do.

    However, when a pupil opens up Garageband, and tries to save a new .band file directly into the redirected symbolic linked folder /Users/Music/Garageband folder it just says permission denied and file is corrupted.

    From the server, pupils have read and write NTFS permissions to the SharedDocuments\Garageband folder, and weirdly when a pupil saves a .band file to their desktop (in 10.7) and drag it into the Mounted SharedDocuments\Garageband folder, it saves - it just will not save via Garageband directly into the mounted folder via the symbolic link.

    Sorry if I haven't explained myself well enough. Any ideas? It's definately some sort of permission problem.

  13. macrumors 6502a

    I'm sorry I can't help you any further with this problem, I've run out of ideas. It may not be related and even be a stupid suggestion but have you looked at this link. You can also start another thread in the dedicated Mac OS X Server, Xserve, and Networking forum section Mac OS X Server, Xserve, and Networking
  14. macrumors newbie

    Kryten, thanks for all your help.

Share This Page