# linux script framework progress about functions encapsulation # Longwei Lai ################################################################### source "${____FWK_SCRIPT_PATH}/lnx_fwk_log.sh" # check specific progress is running or not function is_progress_running() { local pid="$1" local pids="`top -b -n1 | sed -e 's/\s*\(.*\)/\1/; s/\([0-9]*\).*/\1/; /^$/d' | tr -s '\n' " "`" for now_pid in $pids; do if [ "$now_pid" -eq "${pid}" ]; then echo -n "TRUE" return fi done echo -n "FALSE" } # kill specific progress(included child progresses) function kill_progress() { # kill child processes local will_kill_pid="$1" for child in $(ps -o pid --no-headers --ppid ${will_kill_pid}); do kill -9 "${child}" 1>/dev/null 2>&1 done # kill self kill -9 "${will_kill_pid}" 1>/dev/null 2>&1 } # check specific pidfile's progress is running or not function is_pidfile_running() { local pid_file="$1" if [ ! -e "${pid_file}" ]; then echo -n "FALSE" return fi local pid="`cat "${pid_file}"`" if [ $(is_progress_running "${pid}") = "FALSE" ]; then rm -rf "${pid_file}" 1>/dev/null 2>&1 echo -n "FALSE" return fi echo -n "TRUE" } # kill specific pidfile's progress(included child progresses) function stop_pidfile() { local pid_file="$1" if [ "$(is_pidfile_running "${pid_file}")" = "TRUE" ]; then pid="`cat "${pid_file}"`" kill_progress "$pid" sleep 1.618 fi if [ "$(is_pidfile_running "${pid_file}")" = "TRUE" ]; then echo "stop pidfile failed" stop_pidfile "${pid_file}" else rm -rf "${pid_file}" fi }