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.
SIGKILLisn't catchable. The script is flawed. – fd0 May 18 '17 at 09:26