freedev / macosx-script-boot-shutdown

This project is useful to execute a shell script during Mac OS X boot or shutdown.
Apache License 2.0
78 stars 23 forks source link

An automation setup example #7

Open schmidt9 opened 2 years ago

schmidt9 commented 2 years ago

Just sharing example script to automate shutdown setup without original files modification (working with copies), might be appended to readme and be helpful

#!/bin/bash

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
EXECUTABLE_COMMAND="killall Simulator"

#setup paths

REPO_DIR_NAME="macosx-script-boot-shutdown"
REPO_PLIST_NAME="boot-shutdown-script.plist"
REPO_SCRIPT_NAME="boot-shutdown.sh"

REPO_WORKING_DIRECTORY="$SCRIPT_DIR/$REPO_DIR_NAME"
REPO_LOG_PATH=$SCRIPT_DIR

REPO_CUSTOM_PLIST_PATH="$SCRIPT_DIR/$REPO_PLIST_NAME"
REPO_CUSTOM_SCRIPT_PATH="$SCRIPT_DIR/$REPO_SCRIPT_NAME"

#create plist & script copy

cp "$REPO_WORKING_DIRECTORY/$REPO_PLIST_NAME" $REPO_CUSTOM_PLIST_PATH
cp "$REPO_WORKING_DIRECTORY/$REPO_SCRIPT_NAME" $REPO_CUSTOM_SCRIPT_PATH

#replace vars in custom plist

sed -i '' "s#SCRIPT_PATH#$REPO_WORKING_DIRECTORY#g" "$REPO_CUSTOM_PLIST_PATH"
sed -i '' "s#LOG_PATH#$REPO_LOG_PATH#g" "$REPO_CUSTOM_PLIST_PATH"
sed -i '' "s#.*AT SHUTDOWN.*#$EXECUTABLE_COMMAND#g" "$REPO_CUSTOM_SCRIPT_PATH"

#setup launch daemon

sudo cp $REPO_PLIST_NAME  /Library/LaunchDaemons/
sudo launchctl load -w "/Library/LaunchDaemons/$REPO_PLIST_NAME"
freedev commented 10 months ago

Hi @schmidt9, thanks for sharing this.