#!/bin/sh unset KILL SPAWN MODE="t" printhelp () { printf "t: toggle/restart/kill a process\n" printf "t [-tr] [--] process [proc_args]\n" printf "\t-t) toggle (kill if running, start if not)\n" printf "\t-r) restart (kill and start again whether running or not)\n" exit 1; } while getopts "tr-" o; do case "${o}" in t) MODE="t" ;; r) MODE="r" ;; -) break ;; *) printhelp ;; esac done SPAWN="$(echo "$@" | sed 's/^.*-[-tr] //')" KILL="$(echo "$SPAWN" | cut -d' ' -f1)" [ -z "${MODE}" ] || [ -z "$SPAWN" ] && exit case "${MODE}" in t) if pgrep -x "$KILL"; then killall "$KILL" >/dev/null 2>&1 else $SPAWN & fi ;; r) killall "$KILL" >/dev/null 2>&1 sleep 0.1 $SPAWN & ;; esac