1

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.

11
  • Just to be sure of what you're asking, you what a program to start when Apple menu > Restart... is clicked and that program runs and finishes its task before the actual reboot process takes place. Is that correct? What about when a shutdown -r now command, which is a reboot, is issued in a Terminal or from an application? – user3439894 Aug 29 '15 at 16:43
  • That doesn't matter, I only need this program to execute upon the Apple menu > Restart being clicked. – CyrodiilSavior Aug 29 '15 at 16:59
  • As I've mentioned, my method works for me from the GUI and CLI under OS X 10.8.5. What OS X version are you using so I can test under that version? – user3439894 Aug 29 '15 at 17:47
  • I'm using Yosemite, I thought maybe it was a version issue but I wasn't sure. – CyrodiilSavior Aug 29 '15 at 17:50
  • Okay, I'll test with Yosemite a bit later today, need to go cut the grass first. – user3439894 Aug 29 '15 at 17:59
0

It's doable with a reboot proxy, i.e. you'd rename /sbin/reboot, e.g. 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 reboot and 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 reboot and shutdown commands after the original were renamed with a .ori extension. Feel free to adapt it for your use.

Contents of reboot_proxy

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

3
  • Thanks for this, I feel like it will work but for some reason even after replacing the old reboot binary with a new bash script executing what I need it still reboots like normal. Could I be doing something wrong? – CyrodiilSavior Aug 29 '15 at 17:29
  • Actually, when running from the terminal the reboot command works, it seems OSX is using a different command when using the GUI – CyrodiilSavior Aug 29 '15 at 17:31
  • @CyrodiilSavior, The way I have mine setup, whether I click the Apple menu > Reboot... and or Shut Down... commands or run reboot and or shutdown in a Terminal what is in my "# Command(s) to execute before reboot" section all finish before the actual reboot/shutdown occurs. – user3439894 Aug 29 '15 at 17:34
0

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.

1
  • Thats a good suggestion, I'll keep it in mind, I'm going to keep working with @user3439894 and his method for now because I also need to do this on linux systems. – CyrodiilSavior Aug 29 '15 at 20:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .