1

如果我当前的 cronjob 没有运行,它将启动海狸:

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

如果海狸已失效,我想对其进行修改以采取措施(通过重新启动海狸服务,如果失败则将其杀死(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

建议?

4

1 回答 1

1

你可以试试这个:

#!/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
于 2013-09-17T17:26:35.073 回答