Skip to content

Instantly share code, notes, and snippets.

@xingheng
Created January 22, 2019 07:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xingheng/42c9322169c9e19b1a738857dde2f929 to your computer and use it in GitHub Desktop.
Save xingheng/42c9322169c9e19b1a738857dde2f929 to your computer and use it in GitHub Desktop.
Extend the load/unload/reload subcommands for launchctl.
function _launchctl_unload() {
local plist_file=$1
test -f $plist_file || { echo "Invalid launchctl file for unloading!"; return; }
local label_name=$(defaults read $(readlink -f $plist_file) Label)
command launchctl stop $label_name || { echo "Failed to stop the service "$label_name ; return; }
command launchctl unload -w $plist_file || { echo "Failed to unload the file "$plist_file ; return; }
}
function _launchctl_load() {
local plist_file=$1
test -f $plist_file || { echo "Invalid launchctl file for loading!"; return; }
local label_name=$(defaults read $(readlink -f $plist_file) Label)
command launchctl load -w $plist_file || { echo "Failed to load the file "$plist_file ; return; }
command launchctl start $label_name || { echo "Failed to start the service "$label_name ; return; }
}
function _launchctl_reload() {
_launchctl_unload "$@"
_launchctl_load "$@"
}
launchctl() {
case "$1" in
"load")
shift
_launchctl_load "$@"
;;
"unload")
shift
_launchctl_unload "$@"
;;
"reload")
shift
_launchctl_reload "$@"
;;
*)
command launchctl "$@"
;;
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment