The Computer Oracle

How to run a script at login/logout in OS X?

--------------------------------------------------
Hire the world's top talent on demand or became one of them at Toptal: https://topt.al/25cXVn
and get $2,000 discount on your first invoice
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Quiet Intelligence

--

Chapters
00:00 How To Run A Script At Login/Logout In Os X?
00:25 Accepted Answer Score 11
01:24 Answer 2 Score 8
01:56 Answer 3 Score 0
03:35 Thank you

--

Full question
https://superuser.com/questions/295924/h...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#macos #login #logout

#avk47



ACCEPTED ANSWER

Score 11


There are several ways to run scripts at login/logout in OS X, some are more recent and only apply to 10.5 and above, some are rather deprecated, but the fastest one would be to add a Login Hook.

First, create the script you want to run. Open up a Terminal and enter:

touch ~/script.sh
open -e !$

This will open a text editor. Enter the script, e.g. with the following contents:

#!/bin/sh
# insert your script here

Save the file. In your terminal, run:

chmod +x ~/script.sh

This will make the file executable. Now, let's add it as a hook:

sudo defaults write com.apple.loginwindow LoginHook /usr/local/bin/script.sh 

There's also the Logout Hook counterpart:

sudo defaults write com.apple.loginwindow LogoutHook /usr/local/bin/script2.sh

I've tested this on OS X 10.6, and it should work even up to 10.8. Keep in mind that the script runs as root and there is only one hook for login and logout respectively.

To undo all that, enter

sudo defaults delete com.apple.loginwindow LoginHook
sudo defaults delete com.apple.loginwindow LogoutHook

Note that this method is not recommended for deployment or anything, but if you're only using it like your question stated, that should be no problem.




ANSWER 2

Score 8


Login hooks were deprecated in 10.4 in favor of launchd. To run a script at login, save a plist like this as ~/Library/LaunchAgents/test.plist. It's loaded on the next login even if you don't run launchctl load ~/Library/LaunchAgents/test.plist.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
http://www.apple.com/DTDs/PropertyList-1.0.dtd>
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>test</string>
    <key>ProgramArguments</key>
    <array>
        <string>say</string>
        <string>test</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

For more information, see man launchd.plist and this blog post.




ANSWER 3

Score 0


The preferred route is using apple's launchd.

  • launchd .plist files can have, among others, one of the following keys:

    #: man launchd.plist
    
    RunAtLoad <boolean>
        This optional key is used to control whether your job is launched once 
        at the time the job is loaded. The default is false. This key should be 
        avoided, as speculative job launches have an adverse effect on system-
        boot and user-login scenarios.
    
    LaunchOnlyOnce <boolean>
        This optional key specifies whether the job can only be run once and 
        only once.  In other words, if the job cannot be safely respawned 
        without a full machine reboot, then set this key to be true.
    
    StartOnMount <boolean>
        This optional key causes the job to be started every time a filesystem 
        is mounted.
    
  • upon login, launchd will scan through FILES and load the daemons and agents defined therein (via .plist files).

    #: man 8 launchd
    FILES
            ~/Library/LaunchAgents         Per-user agents provided by the user.
            /Library/LaunchAgents          Per-user agents provided by the administrator.
            /Library/LaunchDaemons         System-wide daemons provided by the administrator.
            /System/Library/LaunchAgents   Per-user agents provided by Apple.
            /System/Library/LaunchDaemons  System-wide daemons provided by Apple.
    

therefore:

  • regarding running a script at login:

    1. create a daemon or agent specification/definition (.plist) file
    2. use RunAtLoad .plist key, maybe also use LaunchOnlyOnce
    • if instead you want to run specifically when a certain disk-image/volume/filesystem is mounted, you can use StartOnMount
    1. place the .plist file in one of the appropriate FILES directories

    Now launchd will automatically load it up upon login and run it according to your specification/definition.

  • regarding running a script at logout: you could try running logout stuff upon login, but other than that I don't know of a key that has the functionality of triggering a daemon/agent upon logout, however you could try to find an event that happens upon logout, that launchd can detect, and use that as trigger for your daemon/agent spec./definition.

    • launchd.plist has a key called Sockets, maybe there's a way to do it with that