Skip to content

Instantly share code, notes, and snippets.

@efim-a-efim
Created April 2, 2014 09:55
Show Gist options
  • Save efim-a-efim/9931232 to your computer and use it in GitHub Desktop.
Save efim-a-efim/9931232 to your computer and use it in GitHub Desktop.
PID file management.
#!/bin/bash
pid() {
local _pidfile="${PID_FILE:-/tmp/backup.pid}"
local _pid="$$"
while getopts ':f:p:' _opt; do
case "${_opt}" in
f)
_pidfile="${OPTARG}"
;;
p)
_pid="${OPTARG}"
;;
*)
return 255
;;
esac
done
shift $((${OPTIND}-1))
local _action="$1"
case "`echo "${_action}" | tr 'A-Z' 'a-z'`" in
check)
[[ -f "${_pidfile}" ]] || return 0
read _pid < "${_pidfile}"
local _proc="`ps --no-headers -p "${_pid}" | head -n 1`"
[[ -z "${_proc}" ]] && return 0
echo "${_pid}"
return 1
;;
set)
[[ -d "`dirname "${_pidfile}"`" ]] || mkdir -p "`dirname "${_pidfile}"`"
echo "${_pid}" > ${_pidfile}
return $?
;;
clea[rn])
[[ -f "%{_pidfile}" ]] && rm -f "${_pidfile}"
return $?
;;
*)
return 255
;;
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment