3

This question already has an answer here:

I want to run a script every time when I shutdown my computer. I know this is maybe a duplicate and I really read a lot of similar threads but either the answer isn't working anymore or I don't see how the answer is working.

I already figured out that apple wants me to use launchd. Most of the other solutions don't work anymore anyway.

So I found this answer but as far as I see launchd does everything when the system starts. I will copy the code below

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key><string>boot.shutdown.script.name</string>

<key>ProgramArguments</key>
<array>
  <string>SCRIPT_PATH/boot-shutdown.sh</string>
</array>

<key>RunAtLoad</key>
<true/>

<key>StandardOutPath</key>
<string>LOG_PATH/boot-shutdown.log</string>

<key>StandardErrorPath</key>
<string>LOG_PATH/boot-shutdown.err</string>

</dict>
</plist>

and the script

#!/bin/bash
function shutdown()
{

  # INSERT HERE THE COMMAND YOU WANT EXECUTE AT SHUTDOWN OR SERVICE UNLOAD

  exit 0
}

function startup()
{

  # INSERT HERE THE COMMAND YOU WANT EXECUTE AT STARTUP OR SERVICE LOAD

  tail -f /dev/null &
  wait $!
}

trap shutdown SIGTERM
trap shutdown SIGKILL

startup;

Unfortunately there is no tutorial for launchd which include very basic explanation. So for example the arguments of the key brackets are more than just strings they trigger something but I can't find a list of which words trigger which action.

I would like to understand the basic idea of launchd. Especially which are the commands for running a script when the systems starts and when the systems shuts down.

marked as duplicate by Graham Miln, oa-, fsb, bjbk, Allan May 19 '17 at 0:05

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

I think I would approach this differently to what you're doing, but then again my suggestion is a bit of a workaround compared to what you want.

My approach would be to have your script set up so that it triggers the shutdown as part of its code. In other words, instead of having the Shutdown trigger the script, have the script trigger the shutdown.

The advantage of this is that you can use this as your usual shutdown option if you want, but if you ever need to shutdown without also running your script you can (e.g. if you're having to shutdown multiple times for troubleshooting purposes, or if an installation automatically shuts down your Mac, etc).

If this is an acceptable option for you, then the Applescript code you need is as follows:

tell application "Finder" to shut down

In other words, you effectively need to add the above code to whatever your script is.

Once your script works as you want, I would then create an Automator Service that runs it. This isn't mandatory, but it does mean you'll be able to run your script from the Application menu of any application. And, it also means you can create a keyboard shortcut to trigger it.

If either or both of these options interest you, proceed below.

1. Create a service using Automator

  1. Launch Automator (usually found within your Applications folder)
  2. Go to File > New
  3. Select Service and click Choose
  4. In the top right hand of the window that appears, ensure that "No input" is selected from the Service receives drop-down list
  5. Ensure the In drop-down list is showing "Any application"
  6. In the second column containing the long list, find "Run AppleScript" and double-click on it
  7. This will add the Run AppleScript window on the right
  8. Now, replace the (* Your script goes here *) with your script
  9. Save the service using a meaningful name (e.g. ShutMeDown).

Now you should be able to run the service (and therefore your script) from any application by going to the Services list within any Application menu (e.g. Finder > Services, Safari > Services, TextEdit > Services, etc) and select the service you just created.

If you'd also like to set a keyboard shortcut to trigger things, proceed below.

2. Creating a keyboard shortcut

  1. Go to System Preferences > Keyboard > Shortcuts
  2. Select Services from the sidebar
  3. Find your service (it'll be in the list on the right-hand side)
  4. Add a shortcut by double clicking on the service name
  5. Now go to System Preferences > Security & Privacy > Privacy
  6. Select Accessibility in the sidebar
  7. Click on the + sign (you may need to unlock the padlock if it's locked)
  8. Add Automator.
  9. Add Finder (to find this you will need to navigate to /System/Library/CoreServices/Finder.app).
  10. Exit your System Preferences.

Now you should be able to use the keyboard shortcut to run the service (and therefore your script).

NOTE: I am aware that for some users keyboard shortcuts do not seem to work even though they've been assigned to a service. This is a whole other topic, but if this happens in your case, at least you can run the service from within any application you're using.

If you decide to give this approach a go, feel free to comment me if you have any questions/problems.

0

I will tell what I figured out so far:

As mentioned in this Run script before shutdown you can use a logouthook to achieve the goal. However, apple stopped the support of rc.shutdown and maybe will do the same to logouthook. The alternative apple is offering is launchd but I couldn't find a command which let me execute a script just before logout.

launchd works fine as substitute for a loginhook or cronjobs. Nevertheless there is a workaround so that you can use it instead of a logouthook. The solution is already posted in my question. I just want to mention how it works:

In fact the script will be executed when the computer launches but the trap command waits for the shutdown signal so that the desired function will be executed when the signal is sent.

  • SIGKILL isn't catchable. The script is flawed. – fd0 May 18 '17 at 9:26
  • it still works. okay this means that i can delete this line :) – Nathanael Skrepek May 18 '17 at 10:01

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