Check if hdd is active or not within program

199 views Asked by At

I would like to check if my hard drive is active or not and set a variable or boolean to use later in the program. I have hdparm installed and working properly to spin down the drive after a set period of time. The drive is on /dev/sda2 and automounts at boot.

I read about popen() but am still not great with parsing outputs in c. Is there another way to do this?

Alternatively checking if it is in standby mode will work also.

1

There are 1 answers

1
theamk On

You can just read from "/sys/block/sda2/stat". For documentation see https://www.kernel.org/doc/Documentation/block/stat.txt. You program should work like this:

  1. Open "/sys/block/sda2/stat" with fopen
  2. Use sscanf to parse field 3 (read sectors) and field 7 (write sectors).
  3. fclose the file
  4. wait for few seconds
  5. Repeat steps 1-3. If you got different numbers, the disk was active during that time.