Why does my autostop code not work?

  • I'm trying to cleanly stop the external portable hard drives connected to my LibreElec system. The LE system is a NUC. There are 3 hdd's connected to it.

    The reason I'm trying to cleanly stop the hdd's is because when I shutdown the system, it shuts off too fast. All the portable hdd's are still running during shutdown and they all come to an abrupt halt when LE shutdowns the system. When this happens I can hear the hdd heads stopping and I'm afraid this will lead to failure now or later.

    I was searching this forum before I made my username and found some hepful hints to put some code in autostop.sh file.

    My file is like this:

    Code
       #!/bin/sh
            (
                udiskctl power-off --block-device /dev/sda
                sleep 5s
            )&

    For 1 hdd it works. The connected drive power off, the sleep time i put to allow system to wait till hdd power off works.

    If there are more than 1 hdd attached, i wrote code as follows:

    Bash
    #!/bin/sh
            (
                udiskctl power-off --block-device /dev/sda
                udiskctl power-off --block-device /dev/sdb
                udiskctl power-off --block-device /dev/sdc
                sleep 5s
            )&

    ^ This code doesn't work. The system waits for 5 seconds and all drives shut off at once. I've attached the Kodi log.

    kodi.log

    1. To see logs of autostop.sh and the shutdown process enable Persistent Logging via LibreELEC Settings Addon, reboot, type journalctl -b -1 and scroll almost to the end. journalctl -b -1 -u kodi-autostart may be more selective.
    2. Never use a parallel subshell in autostop.sh. It will be killed after a short time period.

    Edit: Disks are still mounted during autostop.sh. Powering them off is a bad idea.

  • Bash
    #!/bin/bash
    for i in `ls /dev/sd*`; do
      umount -f $i
      sleep 2
      udiskctl power-off --block-device $i
      sleep 2
    done

    ^ That should unmount disks before powering down the drive. Also, you probably want to block the shutdown process while this is done so background the commands like autostart.sh

  • Bash
    #!/bin/bash
    for mount in $(awk '/\/dev\/sd/{print $1}' /proc/mounts); do
      umount -f $mount
      sleep 2
    done
    for disk in $(ls /dev/sd* | sed 's/[p0-9]*//g'); do
      udiskctl power-off --block-device $disk
      sleep 2
    done

    ^ Split into two steps

  • Thank you mglae and chewitt. I tried both the scripts. The first script didn't work and i saw errors in the journal. Then i saw the second script here and overwrote the first script. I'm getting the same errors like in first script.

    Code
    /bin/sh: can't open '/storage/.config/shutdown.sh': No such file or directory
    ......
    /storage/.config/autostop.sh: line 7: udiskctl: not found

    I'm uploading the entire log here i took using journalctl -b -1

    I think Kodi unmounts the devices using ntfs-3g or another service even before the shutdoown script is run. So when the script does run it is throwing errors.

    journal2.txt

    One thing I noticed is that the disks don't make that dreaded noise after putting the shutdown script. They don't power off but the hard disk heads are already parked before they shut down so no noise now.

  • I've blind copied the udiskctl command from your original script. On closer inspection we don't bundle this utility in the OS; hence the failure message that you see. You'll need to sub some other method for turning disks off (or at least parking heads) - perhaps hdparm?