ngandrass / truenas-spindown-timer

Monitors drive I/O and forces HDD spindown after a given idle period. Resistant to S.M.A.R.T. reads.
MIT License
276 stars 28 forks source link

Cron Job not working #35

Closed RenaissanceFlow closed 7 months ago

RenaissanceFlow commented 7 months ago

Hello, I am using TrueNAS Scale 23.10.2. The HDD stanby of TrueNAS system works properly, but I want the drives to only spindown at night to prevent any unnecessary spin up. TrueNAS doesn't have this function, so I use this script instead. My plan is to use /mnt/Download/spindown_timer.sh -d -m -i sda -i sdb -i sdc -i sdd command in the morning to prevent spindown of selected disks. Then I would run /mnt/Download/spindown_timer.sh -m -i sda -i sdb -i sdc -i sdd at night to enable spindown to save power. When I test the script in system shell, it works with sudo. However, after I added Cron Jobs and tried to test it, the Cron Job just stuck at 10% and the script didn't run.

屏幕截图 2024-03-07 115300

This is my Cron Job settings, run as root user.

屏幕截图 2024-03-07 115131

I don't know if this has anying to do with permissions. But I heard running Cron Jobs with sudo is not a good idea as it requires adding admin password in plain text. The Cron Job is supposed to work when the task is run by root. The script is located in /mnt/Download, which is in another SSD, not the HDDs I try to spindown. I already run the chmod mentioned in README before using the script. However, chmod didn't give any feed back and I don't know if that's normal. I blurred the user name in the command because it's my real name, hope you don't mind. At this point I feel so close to have the spindown function I need. I just didn't know what went wrong and I need help. Could anyone give me some advice on what to do? Thanks in advance.

Salvoxia commented 7 months ago

Hi,

how you determine that it's actually not working? The task progress for cronjobs getting "stuck" at 10% is normal in TrueNAS if the cronjob keeps running, such as this script. So if the script will never finish, you'll always have a 10% cron job task in the top bar. And there's one thing I don't understand about your intended usage: If you run one cronjob in the mornings and one in the evenings, you'll start up an additional instance of the script each time, accumulating running cronjobs. So after two full days you would have four instances running, triggered by cronjobs. You would have to make sure to kill the previous instance in the morning and in the evening so that only a single cronjob-triggered instance is running. Then I actually don't see why it wouldn't work. I use the script for shutting down at night, also triggered via cronjob. No issues.

Best Regards, Salvoxia

RenaissanceFlow commented 7 months ago

Hi, Thanks for the reply.

how you determine that it's actually not working?

Because when I use the -d dry run option in the morning, the disks would still spin down.

And there's one thing I don't understand about your intended usage: If you run one cronjob in the mornings and one in the evenings, you'll start up an additional instance of the script each time, accumulating running cronjobs. So after two full days you would have four instances running, triggered by cronjobs. You would have to make sure to kill the previous instance in the morning and in the evening so that only a single cronjob-triggered instance is running.

My bad. I am not familiar with scripts and cron jobs. I guess the problem would be multiple instances of the same script contradicting themselves then. I think I should just kill the instance of script instead of using -d for the disks to not spindown in the day. Is that possible with another cron job? Or is there a better solution for the disks to spindown only at night as I intended?

Salvoxia commented 7 months ago

A new feature called "one shot mode" was introduced to the develop branch for usage with cron jobs. The documentation reads

  -o           : One shot mode. If set, the script performs exactly one I/O poll
                 interval, then immediately spins down drives that were idle for
                 the last <POLL_TIME> seconds, and exits. This option ignores
                 <TIMEOUT>. It can be useful, if you want to invoke to script
                 via cron.

That might be what you're looking for.

RenaissanceFlow commented 7 months ago

With one shot mode now the script works perfectly with cron job. Thanks for the help.