This may seem like a odd question since most people would want to know how to start a program on boot. I was wondering if there is a way to have OSX launch an application when the restart button is pressed from the apple menu.
It's doable with a reboot proxy, i.e. you'd rename
sudo mv /sbin/reboot /sbin/reboot.ori, and then create a
reboot symlink to the
/sbin/reboot_proxy bash script that has the command(s) you what to execute before the reboot and then lastly it executes
/sbin/reboot.ori. I actually have this setup on my Mac running OS X 10.8.5 and it works well. I'm trapping both
shutdown commands. Which are what's run when clicking the Apple menu > Reboot... and or Shut Down... commands.
- Note: It's best to have a second bash script to toggle the state of the original file and the proxy bash script as it has to be set back before OS X updates are done that will touch the file(s).
This is the code in my
shutdown_proxy bash script that has a symbolic link for both the
shutdown commands after the original were renamed with a
.ori extension. Feel free to adapt it for your use.
#!/bin/bash _name="$0"; echo "Reboot Proxy Executing..." # Place command(s) to execute before reboot directly below this comment: _command="$_name.ori" echo "Executing $_command $@..." "$_command" "$@"
Let me add that from a security standpoint there are those that would argue this is a bad thing to do and I'd say to some extent that's true under the right conditions however since I'm the only one that has ever and will ever touch my Mac and I have several layers of security running beyond the built-ins, I personally have no issue using such a method.
Since choosing Apple Menu > Reboot effectively logs you out, you could probably do something like is suggested here: Run Script on Logout Without Use of Logout Hook.
I would avoid attempting to edit or modify any system binaries, scripts, etc and stick to what is available in "userspace." Mostly because any system update could overwrite any changes you made, and if you did anything that if "broken" could prevent booting or logging in... Well, that can be a major headache.
Note: I have not had a chance to put any of that to the test just yet, but it looks like a few leads may be in there.