如果不运行,我当前的cronjob将启动beaver:

#!/bin/bash
pgrep -l -x beaver || /usr/sbin/service beaver start

我想修改它,以便在beaver失效时采取操作(通过重新启动beaver服务,如果失败,则终止它(pkill-u beaver)
# ps -ef | grep [b]eaver
beaver    2890     1  0 Sep15 ?        00:11:27 /usr/bin/python /usr/local/bin/beaver -t sqs -c /etc/beaver/beaver.conf
beaver    2899  2890  0 Sep15 ?        00:03:01 [beaver] <defunct>
# pgrep -l -x beaver
2890 beaver
2899 beaver

建议?

最佳答案

你可以试试这个:

#!/bin/bash
START=false
readarray -t PIDS < <(exec pgrep -x beaver)

function stop_beaver {
    /usr/sbin/service beaver stop
    sleep 5s  ## Optionally wait for processes to stop.
    kill -s SIGTERM "${PIDS[@]}" ## Perhaps force another signal to them if it doesn't work with defuncts.
    sleep 5s  ## Optionally wait for processes to stop.
    kill -s SIGKILL "${PIDS[@]}" ## Perhaps force another signal to them if it doesn't work with defuncts.
    START=true
}
if [[ ${#PIDS[@]} -eq 0 ]]; then
    echo "No beaver process was found."
    START=true
elif [[ ${#PIDS[@]} -eq 1 ]]; then
    echo "Processes found: ${PIDS[*]}"
    echo "Only one beaver process found."
    stop_beaver
elif ps -fp "${PIDS[@]}" | fgrep -F '<defunct>' >/dev/null; then
    echo "Processes found: ${PIDS[*]}"
    echo "Defunct beaver process found."
    stop_beaver
else
    echo "Processes found: ${PIDS[*]}"
fi
[[ $START == true ]] && /usr/sbin/service beaver start

07-27 23:50