-
-
Save efim-a-efim/9931232 to your computer and use it in GitHub Desktop.
PID file management.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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