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

10

4

I'm toying around with building a custom render farm manager, and I want to automatically add OS X machines to the render farm when they are not in use.

Is there a way to trigger a script to run once any user has logged out then stop when any user has logged in?

user36659

Posted 2011-06-11T22:26:33.917

Reputation: 197

Answers

9

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.

slhck

Posted 2011-06-11T22:26:33.917

Reputation: 182 472

doesn't work for yosemite – davidcondrey – 2015-06-15T08:15:19.703

5

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.

Lri

Posted 2011-06-11T22:26:33.917

Reputation: 34 501

According to the linked blog post, this will load the plist the first time you log in, and keep it loaded until the system restarts. How do you use launchd to run a script at every login? Or at logout? – ShadSterling – 2014-08-06T21:58:38.070

-1

For these hooks to work in 10.10 you will need to do this:

  1. Open the /etc/ttys file: In the Finder, choose Go to Folder from the Go menu, type /etc/, then click Go.

  2. In the resulting window, open the ttys file in your preferred text editor (such as TextEdit).

  3. Look for a line that reads:

    console "/System/Library/CoreServices/loginwindow.app/Contents/MacOS/loginwindow" vt100 on secure window=/System/Library/CoreServices/WindowServer onoption="/usr/libexec/getty std.9600"

  4. Edit this line so that it reads as follows (there are no breaks in this line):

    console "/System/Library/CoreServices/loginwindow.app/Contents/MacOS/loginwindow -LoginHook /path/to/script" vt100 on secure window=/System/Library/CoreServices/WindowServer onoption="/usr/libexec/getty std.9600"

    i.e., add -LoginHook /path/to/script (where /path/to/script is the full path to the script that you want to execute when a user logs in) just before the second quote (") mark.

  5. Save the file.

Be sure that the text editor you use to edit this file does not break the line above into more than one line.


or follow full instruction here:

http://support.apple.com/en-ca/HT2420

Dung

Posted 2011-06-11T22:26:33.917

Reputation: 121

doesn't work for yosemite – davidcondrey – 2015-06-15T08:15:08.960