'ensure' -- process assurance for the rest of us
#!/usr/bin/env zsh # # ensure -- a simple process starter # # Usage: ensure[arg1, ...] # # For example: # $ ensure urxvtd -q -f -o # -- Ran 'urxvtd -q -f -o' # $ ensure urxvtd -q -f -o # -- Already running # # But we don't care about arguments, so a further # $ ensure urxvtd -q # -- Already running # # This script is most useful for running daemons like: # urxvtd, mpd, synergys, ... and I use it for this purpose # in my .xsession/.xinitrc file. cmd="${1}" if [[ -z "${cmd}" ]]; then echo "Usage: $(basename $0)" return 1 else if [[ -x $(which pidof) ]]; then pidof "${cmd}" > /dev/null && \ echo "-- Already running" || \ ( $(${*} >/dev/null 2>&1 &) && echo "-- Ran '${*}'" ) else ps xc | tail +2 | awk '{print $5}' | grep "${cmd}" > /dev/null && \ echo "-- Already running" || \ ( $(${*} >/dev/null 2>&1 &) && echo "-- Ran '${*}'" ) fi fi # vim:et:ts=2:sts=2:sw=2:
P.S.
Untested: this should work in most decent shells (bash, zsh, ksh)