![crontab kann den Dienst nicht über ein funktionierendes Skript starten](https://rvso.com/image/617260/crontab%20kann%20den%20Dienst%20nicht%20%C3%BCber%20ein%20funktionierendes%20Skript%20starten.png)
Mein Skript funktioniert, wenn es alleine ausgeführt wird, aber nicht über 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
Betroffen ist das Skript
#!/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
Antwort1
Crond lief nicht.
[root@someserver:]# service crond status
crond is stopped
[root@someserver:]# service crond start
[root@someserver:]# chkconfig crond on
Außerdem habe ich mein Skript wie folgt geändert:
#!/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
Antwort2
Die Antwort darauf interessiert mich sehr, also lassen Sie mich Ihnen bei der Diagnose helfen.
Versuchen Sie, Cron wie folgt zu ändern:
*/1 * * * * /bin/bash -x /opt/scripts/check_beaver_status.sh > /tmp/crondiag.log 2>&1
Fügen Sie außerdem den Befehl hinzu:
env
Sowohl an den Anfang des Skripts als auch an das Ende des Funktionsaufrufs im Skript.
Lass es einmal laufen und teile das Ergebnis mit uns. Vielleicht auf pastie.org oder so.