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?
function check_dev($dev){ $status_ok = "=== START OF READ SMART DATA SECTION ===nSMART 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())."n".$output, FILE_APPEND); if(strpos($output, $status_ok) !== false){ echo "$dev OK!n"; return true; } else{ echo "$dev ERROR!n"; 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
Advertisement
Answer
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.