How to run a script at shutdown with Snow Leopard?

5

3

I want to run a script when I shutdown or restart my iMac, running Snow Leopard.

I read somewhere that one could use the /etc/rc.shutdown.local for this, but it is not working for me.

For example, I put the following lines on it:

#!/bin/sh
/usr/bin/osascript -e "set volume with output muted"

If I run:

source /etc/rc.shutdown.local

it does indeed mute the sound. However, if I have the sound on and make a restart of the OS, the sound will still be on. Which I guess it means the script was not called.

Any ideas?

UPDATE: It is actually working now. I think it's just because the correct name is rc.local.shutdown and not rc.shutdown.local.

user30115

Posted 2011-03-03T21:41:20.190

Reputation: 171

The boot chime is definitely mutable. It follows the mute/volume status of the internal audio device that was active at shutdown. Note, however, that headphone-volume is separate from the internal speaker's volume and modern hardware always plays the statup chime through the internal speaker, not through the headphone jack. – David C. – 2014-09-17T20:33:26.630

This seems a little bit backwards to me. Why don't you just run the muting script at startup? Keep in mind that the boot chime is no longer mutable on current-generation hardware. – NReilingh – 2011-03-04T02:48:46.757

Please add an answer to your own question if you figured it out yourself, and click the checkmark next to it. This will mark this question resolved. – Daniel Beck – 2011-03-05T16:11:14.167

Answers

2

It is actually working now. I think it's just because the correct name is rc.local.shutdown and not rc.shutdown.local.

user30115

Posted 2011-03-03T21:41:20.190

Reputation: 171

1

Another option is to use a logout hook:

sudo defaults write com.apple.loginwindow LogoutHook ~/.logouthook
echo $'#!/usr/bin/osascript\nset volume with output muted' > ~/.logouthook
chmod +x ~/.logouthook

The value of the LogoutHook key can only be a path to an executable and not a shell command. The logout hook is run with superuser privileges so you don't need sudo.

The defaults command modifies /var/root/Library/Preferences/com.apple.loginwindow.plist. Adding a LogoutHook key to /Library/Preferences/com.apple.loginwindow.plist doesn't work.

If a logout hook takes long enough to run, a gray screen is shown until the logout hook terminates. There doesn't seem to be any time limit after which logout hooks are forced to terminate.

Logout hooks were deprecated in 10.4, but I haven't figured out any way to run programs on logout reliably with launchd. I don't know any way to run a logout hook before shutting down or restarting but not before logging out.

Lri

Posted 2011-03-03T21:41:20.190

Reputation: 34 501

0

Use the following script:

#!/usr/bin/env bash
trap 'osascript -e "say \"hello\""' TERM
while true; do
    sleep 10
done

Start it via cron or launchd, whichever you prefer.

Try killing it using kill pid, which pid being its process ID you learn from ps ax.


I don't want to shut down right now to test it propertly, but it should work.

Daniel Beck

Posted 2011-03-03T21:41:20.190

Reputation: 98 421

Shouldn't your osascript line be "set volume with output muted" instead of a say command? – NReilingh – 2011-03-04T02:49:34.010

@NReilingh True. I tested the trap using say and didn't bother to change. – Daniel Beck – 2011-03-04T02:51:50.313

0

I'll start off by saying I'm a noob with AppleScript. So for the benefit of other folks starting off as well - I'm posting the following which is a minor variation of what the author put in.

#!/bin/sh
/usr/bin/osascript -e "set volume 0.5"

Whome

Posted 2011-03-03T21:41:20.190

Reputation: 9