1

I've followed various tutorials and ended up with running smartctl tests from cron.

crontab:

0 2 2 * * /usr/sbin/smartctl --test=long /dev/sda &> /dev/null
0 2 3 * * /usr/sbin/smartctl --test=long /dev/sdb &> /dev/null

I know the long test can take many hours to complete.

I can then run by hand; once it's working, I'll write a script around it:

smartctl -a /dev/sda

I can see if a test is still running, but surely it's not great to constantly call to check results until it's done.

I can see the Power On Hours and also

SMART Self-test log structure revision number 1
Num  Test_Description    Status                  Remaining  LifeTime(hours)  LBA_of_first_error
# 1  Extended offline    Completed without error       00%      1216         -
# 2  Short offline       Completed without error       00%       511         -
# 3  Short offline       Completed without error       00%       487         -
# 4  Extended offline    Completed without error       00%       487         -
# 5  Short offline       Completed without error       00%       464         -
# 6  Short offline       Completed without error       00%       440         -
# 7  Short offline       Completed without error       00%       417         -
# 8  Extended offline    Completed without error       00%       394         -
# 9  Short offline       Completed without error       00%       393         -

But that does not tell me when the results were created.  I can't see Power On, Short / Extended offline will tell me.  I thought about logging Power On Time, but if the system has been rebooted, it's meaningless.

I don't think I'm trying to do anything too odd.  After all, I have a set of results but have no idea when the info they are telling me was created.

I'm sure lots of you know the answer.  Would you please give me some pointers or clues that can help?

4
  • I'm not clear where the results are stored, as you redirect stdout and stderr to /dev/null. You might wrap the smartctl command between two date commands which output to the file where your results come, or send you emails, or some other notify method. Or maybe have the crontab command touch a file that you have set up inotify to wait for. Apr 4, 2022 at 8:08
  • 1
    @Paul_Pedant The test will run in background in any case. There is no relevant output from smartctl -t. Apr 4, 2022 at 8:24
  • @mashuptwice I am reading "I can see if a test is still running ..." and "I have a set of results ...". Those features of the process can surely be wrapped inside tools that report relevant times. Apr 4, 2022 at 16:07
  • @Paul_Pedant that is true, but the output of smartctl -t will not be very helpful with that. It only contains a very rough estimation of the tests length and exits right after starting a test. Apr 4, 2022 at 16:32

1 Answer 1

1

After starting the cronjob you could periodically check the output of smartctl -c for a specific string. If that string exists then you know that currently a test is running. If the string cannot be found, write the date and the output of smartctl -a to a logfile and exit the loop:

while true
do
    sleep 1m
    if smartctl -c /dev/sda | grep "Self-test routine in progress"
    then
        echo "test running"
    else
        echo "no test running"
        #write to log
        date=$(date)
        echo -e "\n\n\nTest completed\n\n\n$date\n\n $(smartctl -a /dev/sda)" >> logfile
        #break out of while loop
        break
    fi
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .