Home:ALL Converter>Running a Shell Script on Shutdown via launchd

Running a Shell Script on Shutdown via launchd

Ask Time:2013-05-14T19:44:21         Author:Haravikk

Json Formatter

Since startup items and rc commands are both deprecated (and in many cases not working at all) on OS X in favour of launchd, I'd like to find out what the correct way would be to setup a shell script that runs on logout/shutdown.

For a startup item it was possible to create a shell script that looked something like:

#!/bin/sh
StartService() {
    echo "Started."
}

StopService() {
    echo "Stopped."
}

RunService "$1"

But the RunService command isn't supported when running a script from launchd, and I'm not sure it's meant to be used anymore anyway.

If possible, I'd like to create a shell script that will be provided to launchd as an on-demand service that will be started near to shutdown and somehow informed that the system is shutting down.

Alternatively, I may need a shell script that is opened on login/system start and remains running (or rather, asleep) until a SIGTERM or other kill signal is received, so that it can run some commands before closing.

Author:Haravikk,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/16542301/running-a-shell-script-on-shutdown-via-launchd
yy