Skip to content

Instantly share code, notes, and snippets.

@examosa
Last active July 2, 2026 15:49
Show Gist options
  • Select an option

  • Save examosa/596f933c836c999cbb4a622c925e20a5 to your computer and use it in GitHub Desktop.

Select an option

Save examosa/596f933c836c999cbb4a622c925e20a5 to your computer and use it in GitHub Desktop.
Shell scripts
#!/usr/bin/env nix
#! nix shell nixpkgs#argc nixpkgs#jq --command bash
# shellcheck shell=bash
# vim: filetype=bash
# @describe Bootstrap a launch agent plist
# @option -c --command! Command to run
# @option -C --cwd <DIR> Working directory
# @option -e --env*, Environment variables
# @option -l --label! Launch agent label
# @option -n --name! Program name
set -euo pipefail
eval "$(argc --argc-eval "$0" "$@")"
shell=${ nix eval --raw 'nixpkgs#runtimeShell';}
readonly \
cmd="${argc_command:-}" \
label="${argc_label:-}" \
name="${argc_name}" \
shell
readonly log_dir="${HOME}/Library/Logs/${name}"
mkdir -p "$log_dir"
declare -Ar config=(
[cmd]="${cmd}"
[Label]="${label}"
[name]="${name}"
[Program]="${shell}"
[StandardErrorPath]="${log_dir}/error.log"
[StandardOutPath]="${log_dir}/output.log"
[WorkingDirectory]="${argc_cwd:-${HOME}}"
)
declare -a jq_args=(--null-input)
for key in "${!config[@]}"; do
value="${config[${key}]}"
jq_args+=(--arg "${key}" "${value}")
done
if [[ -v argc_env && ${#argc_env} -gt 0 ]]; then
env_arg=${
jq --args --null-input --raw-output \
'$ARGS.positional | map(capture("^(?<key>[^=]+)=(?<value>.+)$")) | from_entries' \
"${argc_env[@]}"
}
jq_args+=(--argjson EnvironmentVariables "${env_arg}")
fi
readonly jq_args plist="${HOME}/Library/LaunchAgents/${label}.plist"
jq "${jq_args[@]}" '$ARGS.named + {
ProgramArguments: [
$name,
"-l",
"-c",
"exec \($cmd)"
],
RunAtLoad: true,
KeepAlive: true,
ThrottleInterval: 10,
ProcessType: "Background",
} | del(.cmd, .name)' | plutil -convert xml1 -o "${plist}" -
plutil -lint "${plist}" >&-
domain="gui/${UID}"
readonly domain target="${domain}/${label}"
launchctl bootout "${target}" 2>&- || :
sleep 3
launchctl bootstrap "${domain}" "${plist}"
launchctl print "${target}" | grep -E '^\s*(state|last exit code|program) =' || :
#!/usr/bin/env bash
spinner() {
local pid="$1" message="${2:-Loading…}" delay="${3:-0.1}"
local -a frames=(⠋ ⠙ ⠹ '⠸' ⠼ ⠴ ⠦ ⠧ '⠇' ⠏)
local -i i=0
# Hide cursor
tput civis 2>&-
while kill -0 "$pid" 2>/dev/null; do
printf "\r%s ${message}" "${frames[i]}"
((i = (i + 1) % ${#frames[@]}))
sleep "$delay"
done
# Clear line and restore cursor
printf "\r\033[K"
tput cnorm 2>/dev/null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment