crontab이 작업 스크립트를 통해 서비스를 시작하지 못했습니다.

crontab이 작업 스크립트를 통해 서비스를 시작하지 못했습니다.

내 스크립트는 자체적으로 실행될 때 작동하지만 crontab을 통해서는 작동하지 않습니다.

크론탭

[root@someserver:/]# crontab -l
SHELL=/bin/bash
PATH=/usr/lib64/qt-3.3/bin:/home/ec2/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

*/1 * * * * /bin/bash /opt/scripts/check_beaver_status.sh

문제의 스크립트

#!/bin/bash

function check_if_beaver_running () {
        current_script=`basename $0`
        process_name="beaver"
        /bin/ps aux | /bin/grep "${process_name}" | /bin/grep -v 'grep' | /bin/grep -v "$current_script"

         if [ $? -eq 0 ]; then
             echo "${process_name} running"
         else
             echo "${process_name}: not running, starting..."
             if [ -f /var/run/logstash_beaver.pid ] ; then
                         /bin/rm -f /var/run/logstash_beaver.pid
             fi
             /sbin/service beaver start
         fi
}
check_if_beaver_running

답변1

Crond가 실행되지 않았습니다.

[root@someserver:]# service crond status
crond is stopped

[root@someserver:]# service crond start

[root@someserver:]# chkconfig crond on

또한 스크립트를 다음과 같이 변경했습니다.

#!/bin/bash
# when running in crontab:
# SHELL=/bin/bash
# PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# */10 * * * * /bin/bash beaver_ensure_running.sh

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

답변2

나는 이에 대한 답변에 매우 관심이 있으므로 진단하는 데 도움을 드리겠습니다.

cron을 다음으로 변경해보세요:

*/1 * * * * /bin/bash -x /opt/scripts/check_beaver_status.sh > /tmp/crondiag.log 2>&1

또한 다음 명령을 추가합니다.

env

스크립트 상단과 스크립트의 함수 호출 하단 모두에 적용됩니다.

한 번 실행해보고 결과를 공유해 보세요. 어쩌면 Pastie.org 같은 곳에서요.

관련 정보