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:

/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.

  • 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 Mar 4 '11 at 2:48
  • 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 Mar 5 '11 at 16:11
  • 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. Sep 17 '14 at 20:33

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


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.


Use the following script:

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

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.

  • Shouldn't your osascript line be "set volume with output muted" instead of a say command? – NReilingh Mar 4 '11 at 2:49
  • @NReilingh True. I tested the trap using say and didn't bother to change. – Daniel Beck Mar 4 '11 at 2:51

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.

/usr/bin/osascript -e "set volume 0.5"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.