Skip to content

Instantly share code, notes, and snippets.

@henderea
Last active June 7, 2018 14:55
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 henderea/74016363040a932c3fcac61c7ca7efd7 to your computer and use it in GitHub Desktop.
Save henderea/74016363040a932c3fcac61c7ca7efd7 to your computer and use it in GitHub Desktop.
Manage com.henderea.* services in launchctl
#!/usr/bin/env bash
if [[ $EUID == "0" ]]; then
domain_target="system"
plist_dir="/Library/LaunchDaemons"
else
domain_target="gui/$EUID"
plist_dir="$HOME/Library/LaunchAgents"
fi
function lc_statuschk() {
/bin/launchctl list | grep "com.henderea.$1" >/dev/null 2>/dev/null
return $?
}
function lc_status() {
if lc_statuschk $1; then
echo "Running"
else
echo "Stopped"
fi
}
function lc_list() {
/bin/ls -1 $plist_dir/com.henderea.* | sed 's/.*com[.]henderea[.]\(.\+\)[.]plist/\1/g'
}
function lc_enable() {
if /bin/launchctl enable "$domain_target/com.henderea.$1" >/dev/null 2>/dev/null; then
echo "Enabled"
return 0
else
echo "Failed to enable"
return 1
fi
}
function lc_disable() {
if /bin/launchctl disable "$domain_target/com.henderea.$1" >/dev/null 2>/dev/null; then
echo "Disabled"
return 0
else
echo "Failed to disable"
return 1
fi
}
function lc_start() {
if lc_statuschk $1; then
echo "Already running"
return 0
else
en=$(lc_enable "$1")
if /bin/launchctl bootstrap "$domain_target" "$plist_dir/com.henderea.$1.plist"; then
echo "Started"
return 0
else
echo "Failed to start"
return 1
fi
fi
}
function lc_stop() {
if lc_statuschk $1; then
/bin/launchctl bootout "$domain_target/com.henderea.$1" >/dev/null 2>/dev/null
rv=$?
until [[ $rv -eq 0 || $rv -eq 3 ]]; do
sleep 2s
/bin/launchctl bootout "$domain_target/com.henderea.$1" >/dev/null 2>/dev/null
rv=$?
done
if lc_statuschk $1; then
echo "Failed to stop"
return 1
else
echo "Stopped"
return 0
fi
else
echo "Not running"
return 0
fi
}
function lc_log() {
less "/tmp/com.henderea.$1.stdout"
}
function lc_elog() {
less "/tmp/com.henderea.$1.stderr"
}
function lc_tail() {
tail -f "/tmp/com.henderea.$1.stdout"
}
function lc_etail() {
tail -f "/tmp/com.henderea.$1.stderr"
}
function lc_help() {
cat <<EOM
Usage: $(basename $0) command [args]
Note: com.henderea. is automatically added to the service names given. Don't include it.
Note: run with sudo to work with system daemons. Without sudo, it operates on user agents.
commands:
-h, --help, help Print this help message
status, stat, s Check if the given service is running
list, ls, l List the services
enable, on, e Enable the given service
disable, off, d Disable the given service
start, startup, su Start the given service; enables the service
stop, shutdown, sd Stop the given service; does NOT disable the service
restart, rst Restart the service; runs stop then start
log Start a less with the stdout log file
elog Start a less with the stderr log file
tail Start a tail -f with the stdout log file
etail Start a tail -f with the stderr log file
EOM
}
case "$1" in
-h | --help | help) lc_help;;
status | stat | s) lc_status $2;;
list | ls | l) lc_list;;
enable | on | e) lc_enable $2;;
disable | off | d) lc_disable $2;;
start | startup | su) lc_start $2;;
stop | shutdown | sd) lc_stop $2;;
restart | rst) lc_stop $2; lc_start $2;;
log) lc_log $2;;
elog) lc_elog $2;;
tail) lc_tail $2;;
etail) lc_etail $2;;
*) echo "Unknown command '$1'"
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment