osx - Running a Shell Script on Shutdown via launchd -


since startup items , rc commands both deprecated (and in many cases not working @ all) on os x in favour of launchd, i'd find out correct way setup shell script runs on logout/shutdown.

for startup item possible create shell script looked like:

#!/bin/sh startservice() {     echo "started." }  stopservice() {     echo "stopped." }  runservice "$1" 

but runservice command isn't supported when running script launchd, , i'm not sure it's meant used anymore anyway.

if possible, i'd create shell script provided launchd on-demand service started near shutdown , somehow informed system shutting down.

alternatively, may need shell script opened on login/system start , remains running (or rather, asleep) until sigterm or other kill signal received, can run commands before closing.

@gaige reply! in end went following:

#!/bin/sh startservice() {     echo "started" }  stopservice() {     echo "stopped"     exit 0 }  startservice trap stopservice sigterm while true;     sleep 86400 &     wait $! done 

sleeping full day should prevent wasting processing power, though it's not favourite solution have running that. however, launchd doesn't have criteria seemed allow script run @ shutdown. above instead run on load (login) , captures sigterm on termination, logout or shutdown. note asynchronous use of sleep, environments wait sleep finish before executing trap, no launchd allows couple of seconds respond sigterm before sigkill sent.

as far can tell launchd supports on demand services if they're triggered socket or watched path or whatever, didn't present solution me since still require kind of outside influence.

one beneficial side effect results obtained in startservice, such file pointers etc., can accessed stopservice since it's in memory, no need file saving. doesn't feel elegant solution, hey, if works it's fine!


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -