linux - SH 脚本在控制台中工作但不在 Cronjob 中工作

标签 linux cron raspberry-pi debian

我有一个带有 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 在哪个帐户下执行?

最佳答案

我认为以特定用户身份在 cronjob 中运行 shell 脚本你应该 -u crontab 中的选项。

crontab -u pi -e

在位置文件之前添加命令bash/bin/bash

#<timing> <user> <command> */5 * * * * bash /home/pi/domoticz/scripts/domoticz_state_checker.sh >> /home/pi/domoticz/scripts/domoticz_state_checker.log 2>&1

你可以从所有用户的文件所在的spool中找到crontab

cat /var/spool/cron/crontabs/<user>

关于linux - SH 脚本在控制台中工作但不在 Cronjob 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48189316/

相关文章:

mysql - 使用 Laravel Schedule Cronjob 更新数据库

linux - 在 Bash 输出中获取特定文本

c - 为什么ssh登录时会调用两次pam_sm_authenticate?

bash - 如果进程终止,如何编写 bash 脚本以重新启动进程?

linux - 使用dd创建磁盘镜像时"No space left on device"

java - 在java中通过i2c向寄存器写入0x80或-128时有区别吗

bash - 如何将日期附加到 bash 中的字符串

linux - 使用 G++ 在 Linux 中编写 ping 程序时从哪里开始

linux - Ping 监控 Bash 中的逗号分隔文件?

python - Crontab 没有按预期工作