crontab no puede iniciar el servicio a través del script de trabajo

crontab no puede iniciar el servicio a través del script de trabajo

Mi script funciona cuando se ejecuta solo pero no a través de 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

Guión en cuestión

#!/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

Respuesta1

Crond no estaba corriendo.

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

[root@someserver:]# service crond start

[root@someserver:]# chkconfig crond on

También cambié mi script a:

#!/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

Respuesta2

Estoy muy interesado en la respuesta a esto, así que déjame ayudarte a diagnosticarlo.

Intente cambiar cron a:

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

También agregue el comando:

env

Tanto a la parte superior del script como a la parte inferior de la llamada a la función en el script.

Déjalo funcionar una vez y comparte con nosotros el resultado. Quizás en pastie.org o algo así.

información relacionada