Nov 292012
 

In previous versions of OS X it was easily possible through rc.shutdown.local to run commands before shutdown. I have not been able to use it in Lion or Mountain Lion.

Thankfully even if you restart or shutdown using the Apple menu the UNIX commands are used. This can be misused by replacing the system supplied tools with a self-written proxy script that runs your commands and the executes the original executables.

The script at the bottom is such a proxy. It should be created in /sbin/shutdown_proxy.sh. Then the original shutdown and reboot executables must be renamed to contain the suffix “.old” (shutdown.old and reboot.old). To complete the installation shutdown-proxy has to be symlinked to shutdown and reboot.

Terminal installation instructions

  1. sudo nano /sbin/shutdown_proxy.sh #insert and adjust the code from below
  2. sudo chmod 755 /sbin/shutdown_proxy #make the file executable
  3. sudo mv /sbin/shutdown /sbin/shutdown.old #move the old file aside
  4. sudo mv /sbin/reboot /sbin/reboot.old
  5. sudo ln -s /sbin/shutdown_proxy.sh /sbin/shutdown #link the proxy
  6. sudo ln -s /sbin/shutdown_proxy.sh /sbin/reboot

/sbin/shutdown_proxy.sh

#!/bin/bash
NAME="$0";
echo "Shutdown proxy executing"

# YOUR COMMANDS HERE

COMMAND="$NAME.old"
echo "Will run $COMMAND $@"

"$COMMAND" "$@"

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)