Git Repos / dotfiles / bin / toggle.sh
Last update to this file was on 2024-01-02 at 05:12.
Show toggle.sh
#!/bin/sh
#
# Copyright (c) 2015-2021, Cameron Vanderzanden. Available under the
# terms of the 2-clause BSD license. Please see the file "LICENSE" in
# the root dir of this repository for more information.
#
#- toggle.sh 2015/01/05 cev, last updated 2021/12/07.
## Usage: toggle.sh [-hrstV] program <program options ...>
##
## -h Show help options.
## -r Run program only if not already running.
## -s Stop (pkill) program.
## -t Toggle program; start if not running, stop if running.
## -V Print version info.
##
[ -x $HOME/.local/bin/cev_print.sh ] && . $HOME/.local/bin/cev_print.sh
self="${0##*'/'}"
exe=""
program=""
action="runonce"
die_noargument() {
print_self error
printf "%bno argument.%b\n" "$_norm" "$_clear" >&2
exit 1
}
die_notanexecutable() {
print_noexe "$*"
exit 1
}
# next three functions should be obvious.
check() { pgrep -o -u $USER -q -x $exe 1>/dev/null 2>&1 ; }
stop() { pkill -o -u $USER -x $exe ; }
start() { exec $program 1>/dev/null 2>&1 & }
runonce() {
if check; then
print_self
printf "%b%s%b: already running" "$_high" "$exe" "$_norm"
printf "%b\n" "$_clear"
else start
fi
exit 0
}
toggle() {
if check; then stop
else start
fi
exit 0
}
while getopts "hrstV?" option; do
case $option in
h) print_help && exit ;;
r) action="runonce" ;;
s) action="stop" ;;
t) action="toggle" ;;
V) print_version && exit ;;
"?") print_help && exit ;;
esac
shift $((OPTIND - 1))
done
[ $# -lt 1 ] && die_noargument
# allergic to spaces in binary names / paths to binaries.
exe="${@%%' '*}"
fullexe="$(which $exe)"
program="$@"
[ ! -x "$fullexe" ] && die_notanexecutable "$exe"
case $action in
"runonce") runonce ;;
"stop") stop ;;
"toggle") toggle ;;
esac
Return to the top of this page or return to the overview of this repo.
Log toggle.sh
Date | Commit Message | Author | + | - |
---|---|---|---|---|
2024-01-02 | Change from FreeBSD to Linux, other minor changes | cev | +1 | -1 |
2021-12-13 | Expanded redshift background.sh, misc. changes | cev | +1 | -1 |
2021-12-08 | Initial commit. Dotfiles, scripts, basic Makefile. | cev | +82 |
Return to the top of this page or return to the overview of this repo.