【发布时间】:2018-01-11 16:05:27
【问题描述】:
我有一个带有 Debian 的 Raspberry PI。我也在运行 Domoticz。 Domoticz 有时会停止,我必须重新启动服务。我使用这个脚本(domoticz_state_checker.sh):
#!/bin/bash
echo 'Checking if Domoticz is running.'
DomoticzState=`sudo service domoticz.sh status`
#echo $DomoticzState
if [[ $DomoticzState == *"active (running)"* ]]
then
echo 'Domoticz is running. Nothing to do.'
elif [[ $DomoticzState == *"domoticz is not running ... failed!"* ]]
then
echo 'Domoticz is not running. Restarting Domoticz...'
sudo service domoticz.sh restart
echo 'Domoticz restarted.'
elif [[ $DomoticzState == *"active (exited)"* ]]
then
echo 'Domoticz active (exited). Restarting Domoticz...'
sudo service domoticz.sh restart
echo 'Domoticz restarted.'
elif [[ $DomoticzState == *"inactive (dead)"* ]]
then
echo 'Domoticz inactive (dead). Restarting Domoticz...'
sudo service domoticz.sh restart
echo 'Domoticz restarted.'
fi
当我以 Pi 用户身份运行此脚本时,该脚本有效。我是这样运行的
pi@raspberrypi:~/domoticz/scripts $ /home/pi/domoticz/scripts/domoticz_state_checker.sh
我使用 crontab -e 创建了以下内容以将其作为 cronjob 运行
*/5 * * * * pi /home/pi/domoticz/scripts/domoticz_state_checker.sh >> /home/pi/domoticz/scripts/domoticz_state_checker.log 2>&1
在我的 cron 日志中,我看到正在执行的作业:
Jan 10 14:55:01 raspberrypi CRON[23498]: (pi) CMD (pi /home/pi/domoticz/scripts/domoticz_state_checker.sh >> /home/pi/domoticz/scripts/domoticz_state_checker.log 2>&1)
但脚本不会重新启动我的 Domoticz 服务。检查已完成,但随后我的 if 和 elif 语句出现错误。我在 domoticz_state_checker.log 中看到以下错误:
Checking if Domoticz is running.
/home/pi/domoticz/scripts/domoticz_state_checker.sh: 7: /home/pi/domoticz/scripts/domoticz_state_checker.sh: [[: not found
/home/pi/domoticz/scripts/domoticz_state_checker.sh: 10: /home/pi/domoticz/scripts/domoticz_state_checker.sh: [[: not found
/home/pi/domoticz/scripts/domoticz_state_checker.sh: 15: /home/pi/domoticz/scripts/domoticz_state_checker.sh: [[: not found
/home/pi/domoticz/scripts/domoticz_state_checker.sh: 20: /home/pi/domoticz/scripts/domoticz_state_checker.sh: [[: not found
当我用 suo 执行时
pi@raspberrypi:~/domoticz/scripts $ sudo /home/pi/domoticz/scripts/domoticz_state_checker.sh
我得到与通过 cron 运行脚本时相同的结果
任何想法这里出了什么问题? 我的 cronjob 在哪个帐户下执行?
【问题讨论】:
标签: linux cron raspberry-pi debian