From 055ee8dd90f93ed8e4ec37594fdce3db643b8a90 Mon Sep 17 00:00:00 2001 From: Jeremy Lecour Date: Thu, 26 Sep 2019 09:32:51 +0200 Subject: [PATCH] kill process if really exists, otherwise remove the pid file --- zzz_evobackup | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/zzz_evobackup b/zzz_evobackup index d0225f0..6b48587 100755 --- a/zzz_evobackup +++ b/zzz_evobackup @@ -116,13 +116,18 @@ pick_server() { ## Verify other evobackup process and kill if needed if [ -e "${PIDFILE}" ]; then pid=$(cat "${PIDFILE}") - # Killing the childs of evobackup. - for ppid in $(pgrep -P "${pid}"); do - kill -9 "${ppid}"; - done - # Then kill the main PID. - kill -9 "${pid}" - printf "%s is still running (PID %s). Process has been killed" "$0" "${pid}\n" >&2 + # Does process still exist ? + if kill -O ${pid} 2> /dev/null; then + # Killing the childs of evobackup. + for ppid in $(pgrep -P "${pid}"); do + kill -9 "${ppid}"; + done + # Then kill the main PID. + kill -9 "${pid}" + printf "%s is still running (PID %s). Process has been killed" "$0" "${pid}\n" >&2 + else + rm -f ${PIDFILE} + fi fi echo "$$" > ${PIDFILE} # shellcheck disable=SC2064