« Back to all recent discussions

Nas326 Clean CLI Shutdown

DuracellDuracell Posts: 13  Junior Member
edited April 7 in Questions
I have NAS326 Firmware V5.21 (AAZF.3).  On this I run a small job from a USB stick that monitors an IP address range and, if no boxes are present for 30 minutes, tries to close the server down.

This worked fine on my NAS325v2 using the "poweroff" command. On the NAS326 the execution of the poweroff command leaves the unit idle/inaccessible with all LEDs lit. On restart it does a full disc resync of a 6tb raid! So the box isn't closing cleanly.

Does anybody know which command(s) to use to close the box cleanly. 

I've found the halt command, halt_wrapper script (calls halt) and the /etc/init.d rc.shutdown script (but does this do a shutdown or just the fore work).

Intriguingly, I note from the WEB Interface scripts that there is reference to a shutdown command "/ck6fup6/system.main/shutdown" that is part of the zysh command set. BUT /bin/zysh is a link to /sbin/zyshclient which does not appear to exist?  I did do a su && find . -name zyshclient but it found nowt :-(

Any suggestions would be welcome.  Problem for me is that a 6tb resync = 12hours!  So "playing" can be a tad tiresome.


Best Answer

  • DuracellDuracell Posts: 13  Junior Member
    Accepted Answer
    OK, tested your code and you wont be surprised that on the 326 it worked super.  Still miffed as my code should have worked so the 326 has a sticky usb dongle!  I'll keep digging :-)


  • MijzelfMijzelf Posts: 622  Heroic Warrior Member
    Does the resync problem not popup if you shutdown the box from the webinterface?

    Both poweroff and halt are supposed to close the box cleanly. They both trigger /sbin/init to shutdown, which executes /etc/init.d/rc.shutdown, and, when completed, tells the kernel to halt or poweroff. (On the 325 that boils down to the same action, power off the box. Don't know for sure for the 326).
    If your raid array gets damaged by that action, then I think the power is cut before the disks have flushed their caches.
    In that case it could help to add a sleep to the end of rc.shutdown. If you have serial access, you can look if rc.shutdown spits any errors. When it fails to unmount the raidarray it will not be flushed. Failure can be caused by open files. Open files can be caused by processed which for some reason resist to be killed.

  • DuracellDuracell Posts: 13  Junior Member
    edited April 8
    Mijzelf:  Thanks for your comments; most interesting.
    Yup the web interface doesn't appear to cause a resync (or hasn't yet).  So I think that the poweroff is certainly not closing cleanly - and it's something to do with my code or the usb stick inserted
    What I've got installed is a usb_key_func.sh script that starts a pseudo daemon.  Simply a loop checking (ping) an IP address range.  If non of the pings return true then, after 15 minutes (so the box should be stable), it issues a poweroff command.
    The usb_key_func.sh script simply calls a second script that writes the pseudo daemon script to /tmp.  This is then executed in background while the full fs mount takes place. The script is running as root.
    It's all based around the Entware-ng-stick idea so I thought I'd be on solid ground :-) And it works fine on a NASA325v2.  But not the NAS326. 
    I think the sleep idea sounds interesting. Certainly, giving the box more time to clear down seems sensible And since it is an inactivity shutdown giving it 2 minutes to do it's business is no loss to me!
    I also noted, looking through the Zyxel scripts, that they call halt - so maybe I'll try that as well

    Cheers again

  • DuracellDuracell Posts: 13  Junior Member
    OK so I changed my "daemon" script to call "halt &" instead of "poweroff" and then exited the script with "exit 0".  Further I added a "sleep 60" to rc.shutdown.
    The result was that after the 15 mins inactivity being checked for the script executed "halt &" "exit 0" and left itself idle with all LEDs steady.  On reboot it started a resync!!!
    Prior to my edits I tested the box WITHOUT the usb stick containing the usb_key_func.sh executing an "ssh root: halt" command and this closed the box down without issue?

    Driving me potty this one!
  • MijzelfMijzelf Posts: 622  Heroic Warrior Member
    with all LEDs steady.

    I overlooked that gem in your first post. If the leds are still on, apparently the box fails to poweroff. Are these leds all on when you start the shutdown? Could it be possible that the box is simply hanging in rc.shutdown?

    On my 520 rc.shutdown basically stops the packages, kills system daemons, (smb, ftp, ...) unmounts everything, and set the NIC's to 10mbit.

    The unmounting is done in the sequence which /proc/mounts show. Is your usb stick listed before the raid array? In that case the array is still mounted if for some reason unmount stick fails.

    Can you unmount the stick manually? Can you share the script in usb_key_func.sh?

    BUT /bin/zysh is a link to /sbin/zyshclient which does not appear to exist?

    Are you sure about that? On my 520 it is available. But it's one of the files which at boot is copied from /ram_bin/sbin/ to /sbin/. (And /ram_bin/ is the mountpoint of a big file which is located on md0.) This happens after usb_key_func.sh has run, so theoretically that script can prevent this from happening.

  • DuracellDuracell Posts: 13  Junior Member
    edited April 9
    Thanks again. Sensible comments again.
    Yup all lights on and steady but unable to access the box as the services are closed (ssh, HTTP, telnet!) Not even ping responds which means I have to use the power button. So yup, it's not a clean close.
    I did some tests and if I initiate a halt via a telnet script from my pc "(echo telnet, echo user, echo pass, echo su, echo pass, echo halt ) | telnet". It closes fine!
    This leads me to believe that, like you say, that pesky USB stick is causing this?
    I checked for the zyshclient again. No usb inserted and it's not there! Curious as the symbolic link from /bin/zysh IS still present? Couldn't find the file anywhere? But it IS present on my NAS325v2!
    I checked my /proc/mounts:
    /proc $ cat mounts
    rootfs / rootfs rw 0 0
    /proc /proc proc rw,relatime 0 0
    /sys /sys sysfs rw,relatime 0 0
    devpts /dev/pts devpts rw,relatime,mode=600 0 0
    ubi6:ubi_rootfs2 /firmware/mnt/nand ubifs ro,relatime 0 0
    /dev/md0 /firmware/mnt/sysdisk ext4 ro,relatime,data=ordered 0 0
    /dev/loop0 /ram_bin ext2 ro,relatime 0 0
    /dev/loop0 /usr ext2 ro,relatime 0 0
    /dev/loop0 /lib/security ext2 ro,relatime 0 0
    /dev/loop0 /lib/modules ext2 ro,relatime 0 0
    /dev/loop0 /lib/locale ext2 ro,relatime 0 0
    /dev/ram0 /tmp/tmpfs tmpfs rw,relatime,size=5120k 0 0
    /dev/ram0 /usr/local/etc tmpfs rw,relatime,size=5120k 0 0
    ubi2:ubi_config /etc/zyxel ubifs rw,relatime 0 0
    /dev/mapper/vg_29611905-lv_0f532478 /i-data/0f532478 ext4 rw,noatime,quota,usrquota,stripe=16,data=ordered 0 0
    /dev/mapper/vg_29611905-vg_info_area /mnt/vg_info_area/vg_29611905 ext4 rw,relatime,stripe=16,data=ordered 0 0
    /dev/mapper/vg_29611905-lv_0f532478 /usr/local/apache/htdocs/desktop,/pkg ext4 rw,noatime,quota,usrquota,stripe=16,data=ordered 0 0
    /dev/mapper/vg_29611905-lv_0f532478 /usr/local/mysql ext4 rw,noatime,quota,usrquota,stripe=16,data=ordered 0 0
    /dev/sdb1 /e-data/5224b721e353f0dc5c4cb66897165fc4 vfat rw,relatime,uid=99,fmask=0000,dmask=0000,allow_utime=0022,codepage=437,iocharset=utf8,shortname=mixed,errors=continue 0 0
    configfs /sys/kernel/config configfs rw,relatime 0 0
    One difference with the USB in (highlighted) and that's second from bottom? I would have thought umount of i-data would have removed the raid partitions?

    And just to be complete our rc.shutdowns sound similar:
    /etc/init.d # cat rc.shutdown
    . /etc/profile
    echo -e "\033[033m- `basename $0` start -\033[0m"
    # set system LED to fast blink green
    # shutdown all zypkgs
    /etc/init.d/zypkg_controller.sh stop
    /etc/init.d/zypkg_controller.sh release_env
    # try to umount FS without block waiting
    /bin/umount -f -a -t nfs,smbfs,cifs &
    sleep 1
    # pwr_resume
    if [ ! -e /etc/zyxel/storage/pwron.status ]; then
            /sbin/i2cset -y 0x0 0x0a 0x0a 0x0007 w
    #       pwr_resume disable
    # clear the AF flag of rtc
    #/sbin/rtcAccess clearAF        # no this option in STG-328
    # stop service
    #not to run/mount again
    /bin/killall -9 app_wd myhotplug 2>/dev/null
    /bin/killall pure-ftpd smbd nmbd zylogger 2>/dev/null
    other=`ps|grep -Ev "\[.*\]"|grep -v init|grep -v "\-sh"|grep -v ps|grep -v PID|grep -v ${0##/*/}|grep -v grep`
    echo "start kill." > /dev/console
    echo "${other}" > /dev/console
    kill `echo "${other}"|awk '{print $1}'`
    sleep 5
    #kill -9
    other=`ps|grep -Ev "\[.*\]"|grep -v init|grep -v "\-sh"|grep -v ps|grep -v PID|grep -v ${0##/*/}|grep -v grep`
    echo -e "\n ${other}" > /dev/console
    kill -9 `echo "${other}"|awk '{print $1}'` 2>/dev/null
    sleep 5
    #backup startup-config.conf
    #cp /etc/zyxel/conf/startup-config.conf /etc/zyxel/conf/startup-config.conf.bk
    #for iscsi
    [ -e "$(which target.init)" ] && target.init stop
    # swapoff
    /sbin/swapoff `cat /proc/swaps|awk '{print $1}'|grep -v Filename`
    # umount
    /bin/umount `cat /proc/mounts|grep /dev/md|awk '{print $2}'` 2>/dev/null
    /bin/umount `cat /proc/mounts|grep /dev/sd|awk '{print $2}'` 2>/dev/null
    /bin/umount `cat /proc/mounts | grep "/dev/mapper/vg_" | awk '{print $2}'` 2>/dev/null
    /usr/sbin/vgchange -an
    /bin/umount /etc/zyxel
    mdadm -Ss
    # debug info
    cat /proc/mounts > /dev/console
    # umount config/rootfs partitions
    /bin/umount `cat /proc/mounts|grep ubi|awk '{print $2}'` 2>/dev/null
    ubidetach -m ${CONFIG_MTD_NUM}
    #umount /firmware/mnt/nand
    ubidetach -m `${INFO_PRINTENV} sysimg_mtd_1 | awk -F= '{print $2}'`
    ubidetach -m `${INFO_PRINTENV} sysimg_mtd_2 | awk -F= '{print $2}'`
    # set PHY speed to 10M to reduce power consuming
    ethtool -s egiga0 autoneg off speed 10 duplex half
    #ethtool -s egiga1 autoneg off speed 10 duplex half             # no egiga1 in STG-328
    ethtool egiga0
    #ethtool egiga1         # no egiga1 in STG-328
    echo -e "\033[033m- `basename $0` end -\033[0m"

    Finally, even with the stick installed a BUTTON shutdown is fine ... so the script to close is the issue?
  • DuracellDuracell Posts: 13  Junior Member
    I'll be candid and post the script that the usb_key_func.sh starts:
    # This is the USER executeable script for the universal_usb_key_func
    # autorun script facility on Zyxel NAS server.
    # It is called by usb_key_func.sh and starts user Sleep On Idle Daemon.
    # Functions
        # This is the work load section
        # It will be called by stage 2 from the copy of this script running in background
        # It is this function that starts SOID
        touch /tmp/usbscript.sh.log
        echo $(date) usbscript.sh--STARTS.IN.TMP > /tmp/usbscript.sh.log
        /tmp/soid &
        echo $(date) usbscript.sh--ENDS.IN.TMP >> /tmp/usbscript.sh.log
        # Wait for the harddisk to be mounted
        while [ 1 ]; do
            if cat /proc/mounts | grep /dev/md0 ; then
                return 0
            sleep 5
        # This function sets up the SOID daemon in the tmp directory
        touch /tmp/soid
        chmod +x /tmp/soid
        cat > /tmp/soid << EOF
    # Note on initial startup the clocks have yet to be synchronised and
    # carry the wrong time. So sleep 5 minutes for clocks to correct
    sleep 300
    touch /tmp/soid.log
    echo \$(date) SOID_RUNNING > /tmp/soid.log
    # Variables
    STARTTIME=\$(date +%s)
    ENDTIME=\$(date +%s)
    # Just loop while running
    while true; do
    # Loop through ips       
        while [ \$HOSTADDR -lt 60 ]; do
            if [ "\` ping -c 2 \$FULLIP | grep "2 packets received" \`" ]; then
                echo \$(date) SOID_FOUND: \$FULLIP >> /tmp/soid.log
                STARTTIME=\$(date +%s)
                ENDTIME=\$(date +%s)
    # Test if anything there
        if [ \$DEVICEON -eq 0 ]; then
            echo \$(date) SOID_NODEVICE >> /tmp/soid.log
            ENDTIME=\$(date +%s)
        if [ \$DIFF -gt 900 ]; then
            halt &
            exit 0
    # 5 minutes wait to loop   
        sleep 300
        # This is the main function
        # Stage 1 creates the SOID Daemon and makes a copy of this script so OS loading can be continued
        # The copy will be started in background and will initiate the SOID daemon
        local command=$1
        case "$command" in
            # leave a trace
            echo usb_key_func_RUNS > /mnt/parnerkey/usb_key_func.log
            # Create soid
            # Copy script to /tmp
            cp $0 /tmp/usbscript.sh
            # Start it in background
            /tmp/usbscript.sh stage2 >/dev/null 2>&1 &
            # continue /etc/init.d/rcS
            exit 1
            echo "This script is for internal use of the SOID daemon"
    StickMain "[email protected]"

    As a telnet session halt, an ssh [email protected] halt, a (echo blah blah) | telnet and a button push all close OK.  HOLD ON .... just did a button close with USB in and it's hung!!! But no resync after a power switch off! So the i-data's were down by then.
    This is getting weird - I might try another USB stick to see if that does the same?
  • MijzelfMijzelf Posts: 622  Heroic Warrior Member
    Not even ping responds

    I *think* this means it's not a hanging umount. Ping is handled at kernel level, or even in the NIC itself, depending on hardware capabilities. So it's not dependent on a daemon. A kernel panic?

    This is getting weird - I might try another USB stick to see if that does the same?

    It won't hurt.

    I don't see anything in your script which can cause this strange behaviour. I see no open files left on the stick.

    Your stick is /dev/sdb. Isn't that strange? I assume you have 2 disks, so that is sda and sdc? Normally a stick in a 2 disk system is either sda or sdc, depending on which bus is scanned first.

    Are both disks healthy, according to smartctl?

  • DuracellDuracell Posts: 13  Junior Member
    Checked the smart info and no reported problems. Pretty standard with both stated as good.
    I didn't see the sdb1 for the usb (getting old!). So I did a little test and put the stick in after boot ... it came up as sdc1? There is only one stick in at a time? I tried all three usb slots but always sdc1. Odd.
    Give me a day or two and I'll try another stick to see what happens.

  • MijzelfMijzelf Posts: 622  Heroic Warrior Member
    I tried all three usb slots but always sdc1. Odd.

    No. It's to be expected. By default a new scsi device gets the first available free sd node. (And all sata disks and usb mass storage devices use some scsi compatibility layer).

    If you remove a stick the node is freed, and when you plug it in again the node is reused. It doesn't matter how you connect it.

    I was surprised that both harddisks didn't have contiguous nodes, as expected by timing at boot.

  • DuracellDuracell Posts: 13  Junior Member
    edited April 14
    Well I'm stumped. Tried another USB stick and the same result.  The box was left with all LEDs lit but no way (telnet, http, etc) to access it.  Obviously the services had closed but the box did not fully shutdown.
    So it was closed by holding the power button until it died. Then, of course, on reboot the discs started to resync.
    I can still close the box successfully via a telnet session calling "halt"? So something isn't umounting. But as the box rewrites it's file store I cant think how to get logs saved so I can see what is happening.
  • MijzelfMijzelf Posts: 622  Heroic Warrior Member
    But as the box rewrites it's file store I cant think how to get logs saved so I can see what is happening.

    The only way I can think of is the serial port. Theoretically you could log to a flash partition, but by doing so you are (seriously?) influencing the shutdown.

    When you shutdown over telnet, with stick plugged and script running, everything is OK. How about letting the script shutdown the box using telnet?

    Have you looked at my shutdown daemon in Tweaks? It does about the same, only it's called once a minute from crond, instead of polling. Oh, and it calls poweroff.

  • DuracellDuracell Posts: 13  Junior Member
    edited April 16
    Thanks again. Thought about the serial interface but I'd have to set up a console.
    I'll have a look at your code, ta ... and I hadn't thought of telneting from the box itself :-) Worth a play.
    And while I was contemplating my fate I knocked up a "Network Monitoring Box" on an old RaspberryPI 1B that was sitting idle in my bits box. I put "raspbian stretch lite" on, apt installed loads of network tools. I wrote a small service that pings the network boxes and WOLs & telnet halts my Zyxels on "network idle". It only took a couple of hours to knock up and it could be a fun tool. And I would be able to remove my WOL daemon from all boxes and my watch&start code on the router (for the TV's - no WOL on a TV!).
    I put a listing at the end for a hoot! Surprisingly it worked!! After 3.5 decades in the business (& retired for 10 - yup I'm an IT dinosaur!) I never trust my own quick code to do that!!
    I'll still like to know what the disk issue is ... may be its some form of incompatibility with the hard drives? 2 * 6tb Seagate ST6000VN0033 IronWolf Nas. As I say the same code is fine on a NAS325v2. Same USB stick model but different hard drives. The firmware is updated of course (eg PHP is not complete on the 325 - no PDO, but ok on 326!) so it may be an issue in the 326 firmware.

    # Rodina.Dum Network Monitor Service
    # This service pings the Rodina.Dum Network Ip address for media and
    # pc clients in the range
    # When any of these clients appear on the network the server will
    # initaiate a WakeOnLan pings for the 3 NAS servers at
    # &
    # If no IP addresses in that range respond within a 10 minute session
    # WOL pings will be stopped  and the NAS boxes closed.
    # Note that & are Zyxel Nas boxes
    # and require manual closing via a telnet command. is a
    # Buffalo server and self closes when WOL pings are not received
    # Variables
    STARTDAY=$(date +%d);
    ENDDAY=$(date +%d);
    STARTTIME=$(date +%s);
    ENDTIME=$(date +%s);
    RCMD="halt &";
    #Change Log file on startup
    savelog -l -t -c 5 $LOGFILE;
    echo $(date) "NetMonD starting" >> $LOGFILE;
    echo $(date) "NetMonD log rotated" >> $LOGFILE;
    # start of main ETERNAL loop
    while true; do
    # Check to see if the log file needs to be rotated
      ENDDAY=$(date +%d);
      if [ $ENDDAY -ne $STARTDAY ]; then
    # Date has changed
    # Reset the day counter
        STARTDAY=$(date +%d);
    # Rotate the logs
        savelog -l -t -c 5 $LOGFILE;
        echo $(date) "NetMonD Continues" >> $LOGFILE;
        echo $(date) "NetMonD log rotated" >> $LOGFILE;
    # Loop Ping to see if any of the monitored ip range boxes are on
      while [ $HOSTIP -lt 60 ]; do
        if ping -c 2 $FULLIP; then
          echo $(date) "$FULLIP ON = WOL" >> $LOGFILE;
          wakeonlan c8:54:4B:78:ae:04;
          wakeonlan 4c:9e:ff:40:96:77;
          wakeonlan -i -p 2304 4c:e6:76:96:bb:bc;
    # Set device on flag and reset both time counters     
          STARTIME=$(date +%s);
          ENDTIME=$(date +%s);
    # We now know if anything is switched on
    # And if so we have sent wake up pings to the servers
    # But if DEVICESTATUS=0 then nowt is switched on
      if [ $DEVICESTATUS -eq 0 ]; then
    #  Set end time only as no device is on
        ENDTIME=$(date +%s);
        echo $(date) "Nothing found" >> $LOGFILE;
    # if 10 minutes has passed it time to halt the Zyxel boxes
    # This is done by a telnet session to call the halt command
      if [ $TIMEDIFF -gt 600 ]; then
    # Stop the ever growing number
        if ping -c 2 $RNASIP0; then
    # Zyxel 0 is up so shut it   
          ( echo open ${RNASIP0}
          sleep 2
          echo ${RUSER}
          sleep 2
          echo ${RPASS}
          sleep 2
          echo ${RCMD}
          echo $(RBYE)
          ) | telnet;
          echo $(date) "NetMonD $RNASIP0 Telnet shutdown issued" >> $LOGFILE;
          echo $(date) "as no devices discovered for 10 minutes" >> $LOGFILE;
      if ping -c 2 $RNASIP1; then
    # Zyxel 1 is up so shut it
          ( echo open ${RNASIP1}
          sleep 2
          echo ${RUSER}
          sleep 2
          echo ${RPASS}
          sleep 2
          echo ${RCMD}
          echo $(RBYE)
          ) | telnet;
          echo $(date) "NetMonD $RNASIP1 Telnet shutdown issued" >> $LOGFILE;
          echo $(date) "as no devices discovered for 10 minutes" >> $LOGFILE;
    # Buffalo will do itself :-) and telnet is closed (SSH open)
    # So after a complete round we wait a minute
    sleep 60;
    #End of main loop

  • DuracellDuracell Posts: 13  Junior Member
    The plot thickens!  Now I have a friend who has a NAS326 with 2* 1tb Seagate IronWoilf Nas drives.  I put the same shutdown code as mine on the same model usb stick and YUP it failed to close down cleanly!  So either a) the Seagate IronWolf drives are a problem as a range, b) that model of USB stick wont umount (my alternate stick didn't either but the NAS may just be USB fussy), or c) the NAS has issues about USB initiated (only way?) internal scripts calling poweroff.
  • MijzelfMijzelf Posts: 622  Heroic Warrior Member
     (only way?)

    Certainly not. I wrote the 'RandomTools' package, which, among other goodies, supplies a 'init.d' like directory in which the scripts are executed on package start.

  • DuracellDuracell Posts: 13  Junior Member
    Ah well in that case you wouldn't like to write a Sleep.On.Idle.Daemon per chance :-).  I did but the thing don't work :-) Story of my life!
    The problem I couldn't quickly over come (NAS325v2) was how to detect a lack of WOL Magic Packets. Too low down in the network stack?  That's why my code just pings an IP range to see if anything's on (Quick, dirty and effective!).  Other than that shutting down seemed a piece of cake until I bought the NAS326!
    I think the issue will remain if I have a USB stick inserted. So it would need to be hosted either as a package install or on a small share on the main drives.
    Any thoughts on how I would go about putting it on the hard drives rather than USB would be most welcome.  If nothing else, as a dinosaur, that fact that I have 8gb drive (cheapest) using 86kb is criminal! (But in odd symmetry, the first mainframe I was a os programmer on had 8gb of disc - it filled a very large room!) 
Sign In or Register to comment.