Shell script idea

Posted:
in Genius Bar edited January 2014
I have been playing with this new little app:



http://www.afp548.com/article.php?st...41226165854372



I am trying to figure out a way to launch it when ANY user logs on to their Mac. Unlike the "Login Items" in the OS X User Account System Prefs Pane (which is user-specific and not global), I need the app to launch no matter who logs on. I dont want to have to manually add this app to every user account on every Mac I admin. Thus, making a shell script launch it at login time for every user is my goal.



Since I need to run an actual application (not a service), it cant reside in /Library/Startup Items, as it needs to run (briefly) with a GUI that users can see. It needs to be a Login Item as opposed to a Startup Item. My challange is to write a script that will run it every time any user logs in.



I have written the script in various ways during my experimenting (Im not too bad at scripting when I apply myself), but I cant figure out what event will trigger the app to launch. Its been trickier than I thought. I cant figure out what even would trigger the script to launch the app at the right time. For example, If I just lauch the app from a shell script when a user logs ins, the app lauchs too fast before the user even gets to the Finder. Thus, any interface or prompt the user might need to see from the app never shows up. If I run the same script mAnually once a user is logged in, then it runs fine. I tried making the script "sleep" for 30 seconds to give the Finder time to load, but the "slep" command just delayed the login process by 30 seconds. It didnt do what I wanted. I tried to make a conditional that did this:



#!/bin/sh



if

ps -aux | grep Finder; then

open /Applications/Utilities/Keychain\\ Minder.app

exit 0

fi

exit 0



But since it only runs once, it exits if the Finder isnt launched yet. I need help writing the loop that would KEEP checking for the Finder, THEN run the app, THEN exit. I havent had luck with the loop logic yet.



I dont want it to run as a cron task. It only needs to run at login time.



If anyone has a similar login script, or any ideas in general, please let me know!



What does the app do you ask? Its a simple too that checks to see if a users Active Directory pasword is the same has his/her Keychain password. If its not, then it prompts the user to enter the new AD passsword and it synchs with the KEychian. This app is so cool for me. I admin 200 MAcs, and I get Keychain help calls ALL THE TIME due to AD forcing a new password and the Keychain not synching properly. IF I can push this app out (along with a shell script) via aARD to my 200 Macs, then I have just saved myself a ton of help calls, not to mention helped out 200 annoyed Mac users to manage their Keychain themselves!

Comments

  • Reply 1 of 8
    mcqmcq Posts: 1,543member
    Not quite sure if this is what you're looking for, but I started to try and play with the code you posted. Done very little shell scripting, so this is probably bad code



    I couldn't really get the "ps -aux" to show a certain app reliably, so I used top instead. I used a loop for getting it to work, and a status variable to check it. My example below waits for Firefox to open, and then opens TextEdit when it determines Firefox is open. You can modify it to whatever you want. The echo was for debug purposes, you can likely remove that too.



    My guess is this could be condensed, but someone can probably just post a better way.



    #!/bin/sh



    lastStatus=1

    until [ $lastStatus -eq 0 ]

    do

    top -l 1 | grep firefox

    lastStatus=$?

    echo "$lastStatus"

    done



    open /Applications/TextEdit.app

    exit 0
  • Reply 2 of 8
    dstranathandstranathan Posts: 1,717member
    Quote:

    Originally posted by MCQ

    Not quite sure if this is what you're looking for, but I started to try and play with the code you posted. Done very little shell scripting, so this is probably bad code



    I couldn't really get the "ps -aux" to show a certain app reliably, so I used top instead. I used a loop for getting it to work, and a status variable to check it. My example below waits for Firefox to open, and then opens TextEdit when it determines Firefox is open. You can modify it to whatever you want. The echo was for debug purposes, you can likely remove that too.



    My guess is this could be condensed, but someone can probably just post a better way.



    #!/bin/sh



    lastStatus=1

    until [ $lastStatus -eq 0 ]

    do

    top -l 1 | grep firefox

    lastStatus=$?

    echo "$lastStatus"

    done



    open /Applications/TextEdit.app

    exit 0




    Sweet. I will play with it.



    I didnt know you can grep top (Since it runs forever by default). I never used the "-l 1" before. Very Intresting. I love learning this stuff. Its a blast. Thanks
  • Reply 3 of 8
    What you are looking for needs to be implemented as a login hook. If you google for "macos x login hook" you will get plenty of help.
  • Reply 4 of 8
    dstranathandstranathan Posts: 1,717member
    Quote:

    Originally posted by Karl Kuehn

    What you are looking for needs to be implemented as a login hook. If you google for "macos x login hook" you will get plenty of help.



    I have been all over those sites. For some reason, my situation is unique. Since I want an actual app with a GUI to run, most examples I have found dont work (they are usually focused on running a low level system command or task, which is faceless and can run regardless of who is logged in, etc)



    All my test scripts work if I run them once a user is logged in, but if I make my script(s) automated to run at login, the run too early or too fast I think. Before the login window progress bar is even done spinning, the app is allready done running and thus the user doesnt see it. I need it to run a little later. I have tried using the "sleep" timer command, but it literally makes the login preocess sleep for x seconds and doesnt help me. LOL



    The search continues.
  • Reply 5 of 8
    dstranathandstranathan Posts: 1,717member
    Quote:

    Originally posted by MCQ

    Not quite sure if this is what you're looking for, but I started to try and play with the code you posted. Done very little shell scripting, so this is probably bad code



    I couldn't really get the "ps -aux" to show a certain app reliably, so I used top instead. I used a loop for getting it to work, and a status variable to check it. My example below waits for Firefox to open, and then opens TextEdit when it determines Firefox is open. You can modify it to whatever you want. The echo was for debug purposes, you can likely remove that too.



    My guess is this could be condensed, but someone can probably just post a better way.



    #!/bin/sh



    lastStatus=1

    until [ $lastStatus -eq 0 ]

    do

    top -l 1 | grep firefox

    lastStatus=$?

    echo "$lastStatus"

    done



    open /Applications/TextEdit.app

    exit 0




    This was funny. I modified your script and ran it as a login hook. It runs so early at login that the Mac wont EVER log a user in until Finder starts. The "barber pole" bar spins forever. LOL. I can kill it from single user mode. The loop ran forever! Since Finder never started, it contiuned to loop on and on and on LOL
  • Reply 6 of 8
    mcqmcq Posts: 1,543member
    lol oops!



    I didn't test it for login stuff, so I had no clue how it would act. I just assumed that your initial script worked, so it would start at whatever time it needed to. I had only tested it while I was logged in.



    So, you're saying that after the user enters their login stuff, this thing gets executed before the rest of the startup processes? That sucks.
  • Reply 7 of 8
    mcqmcq Posts: 1,543member
    Did you see this article? May be easier this way.

    http://macenterprise.org/content/view/105/77/



    Sounds like you can just add an entry into the /Library/Preferences/loginwindow.plist file to launch the app.
  • Reply 8 of 8
    dstranathandstranathan Posts: 1,717member
    Reading now...thanks!
Sign In or Register to comment.