【问题标题】:SH Script working in console but not in CronjobSH 脚本在控制台中工作,但在 Cronjob 中不工作
【发布时间】: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


    【解决方案1】:

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

    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>

    【讨论】:

    • 看起来作业在 pi 帐户下,但是当使用 suo 运行时?为什么我不能用 sudo 运行我的脚本
    • 使用 crontab -u pi -e 提供与 crontab -e 相同的工作
    • 当我运行命令 sudo bash /home/pi/domoticz/scripts/domoticz_state_checker.sh 我没有得到错误。 bash 是做什么的?我是 Linux 新手
    • @Danny,对不起,我忘了告诉你,如果你用 sudo 运行脚本,就像你以 root 用户运行脚本一样。在没有sudosudo 的情况下运行任何应用程序是不同的,sudo 允许用户以另一个用户(超级用户/root)身份运行程序。如果你想用 sudo 运行,你可以在 crontab 命令中将用户 pi 更改为 root (crontab -u root -e)
    • 现在很清楚了。谢谢。但是我不明白为什么脚本在使用 sudo 运行时失败但在作为 pi 运行时工作。
    猜你喜欢
    • 2015-04-09
    • 1970-01-01
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2013-08-27
    • 1970-01-01
    相关资源
    最近更新 更多