dongyingming8970 2015-06-17 10:43
浏览 41
已采纳

smartctl没有从cron工作

I have a problem with smartctl

Have updated from version 5.4 to the lastest version 6.4.. No change.. Same issue

It works fine when running the command smartctl -H /dev/sda directly from the command line

But when running the command from a cronjob its not working as it should. Here you can see the cron job settings.. Its running as root

The job is running every 60 sec while testing and the command doesn't return anything. Only the timestamp is written to the file.

But if I press the button "Run now" then the command works?! Very strange!? In the code you can see that the output is written to a file.. The output is empty

Another thing.. When the job is automated only one timestamp is written to the file.. Two timestamps should be written?

enter image description here

function check_dev($dev){
    $status_ok = "=== START OF READ SMART DATA SECTION ===
SMART overall-health self-assessment test result: PASSED";

    $output = shell_exec('smartctl -H '.$dev);

    file_put_contents('/var/www/hdd_out.txt', gmdate("M d Y H:i:s", time())."
".$output, FILE_APPEND);

    if(strpos($output, $status_ok) !== false){
        echo "$dev OK!
";

        return true;
    }
    else{
        echo "$dev ERROR!
";

        return false;
    }   
}

if(check_dev('/dev/sda') && check_dev('/dev/sdb')){
    $status = 0;
}
else{
    $status = 1;
}

output file

Jun 17 2015 10:17:01
Jun 17 2015 10:18:01
Jun 17 2015 10:19:01
Jun 17 2015 10:20:01
Jun 17 2015 10:21:01
Jun 17 2015 10:22:01
Jun 17 2015 10:23:01
Jun 17 2015 10:24:01
Jun 17 2015 10:25:01
Jun 17 2015 10:26:01
Jun 17 2015 10:27:01
Jun 17 2015 10:28:01
Jun 17 2015 10:29:01
Jun 17 2015 10:29:54 # here I manually pressed "Run now"
smartctl 6.4 2014-09-29 r3990 [x86_64-linux-2.6.32-5-amd64] (local build)
Copyright (C) 2002-14, Bruce Allen, Christian Franke, www.smartmontools.org

=== START OF READ SMART DATA SECTION ===
SMART overall-health self-assessment test result: PASSED

Jun 17 2015 10:29:54 # here I manually pressed "Run now"
smartctl 6.4 2014-09-29 r3990 [x86_64-linux-2.6.32-5-amd64] (local build)
Copyright (C) 2002-14, Bruce Allen, Christian Franke, www.smartmontools.org

=== START OF READ SMART DATA SECTION ===
SMART overall-health self-assessment test result: PASSED

Jun 17 2015 10:30:01
Jun 17 2015 10:31:01
Jun 17 2015 10:32:01
Jun 17 2015 10:33:01
  • 写回答

1条回答 默认 最新

  • dousikuai5417 2015-06-17 13:25
    关注

    The user and environment under which cron runs is often much more limited than the environment you're used to when logged into a TTY. In particular, the $PATH environment variable may be different or even empty.

    It is therefore recommended to use the full path to any executable you call in the script. Since the cron script is running, the php executable is being found in the cron task itself, but the smartctl may not be found inside the PHP script.

    Locate the full path to smartctl using which smartctl, then modify the PHP script to use the full path. It is likely /usr/bin/smartctl or /usr/sbin/smartctl.

    Using shell_exec() makes it difficult to retrieve error information from commands called. Instead, using exec() and specifying the 3rd argument $return_var to capture the return code, along with the $output array may be more helpful.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘