Announcement

Collapse
No announcement yet.

Linux Shutdown and Restart commands don't work

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Linux Shutdown and Restart commands don't work

    What do I need to check or change to ensure that the HS3 web interface Tools/System/Shutdown and Tools/System/Restart actually work?

    Seems that HS3 stops, but nothing else (so server is left in non-operational state).

    Thanks,

    A

  • #2
    Here I have only used the shutdown Homeseer command since the days of the original Zee.

    Many times here shut down Homeseer via the HS gui and then reboot via ssh.

    You can give the scripts a try to see if they work via ssh or a local terminal session.

    Changing the timing in the scripts may help you.

    reboot script reads:

    #!/bin/bash


    counter=20
    while [ $counter -gt 0 ]
    do
    sleep 1
    counter=$(($counter - 1))
    done

    # now restart system
    reboot




    shutdown script reads:

    #!/bin/sh
    sleep 5
    shutdown now

    shutdown controller script reads:

    #!/bin/bash

    # check is a process is running, mono should be the HomeSeer process
    check_process() {
    # echo "$ts: checking $1"
    [ "$1" = "" ] && return 0
    [ `pgrep -n $1` ] && return 1 || return 0
    }

    # wait for the mono process to exit, HomeSeer will then be shut down
    while [ 1 ]; do
    check_process "mono"
    # echo "$?"
    if [ $? == 0 ]; then
    break
    fi
    sleep 2
    done

    # now power off the system
    poweroff
    - Pete

    Auto mator
    Homeseer 3 Pro - 3.0.0.534 (Linux) - Ubuntu 18.04/W7e 64 bit Intel Haswell CPU - Mono 6.00
    Homeseer Zee2 (Lite) - 3.0.0.534 (Linux) - Ubuntu 18.04/W7e - CherryTrail x5-Z8350 BeeLink 4Gb BT3 Pro - Mono 6.00

    X10, UPB, Zigbee, ZWave and Wifi MQTT automation.

    Comment


    • #3
      Originally posted by Pete View Post
      Here I have only used the shutdown Homeseer command since the days of the original Zee.

      Many times here shut down Homeseer via the HS gui and then reboot via ssh.

      You can give the scripts a try to see if they work via ssh or a local terminal session.

      Changing the timing in the scripts may help you.

      reboot script reads:

      #!/bin/bash


      counter=20
      while [ $counter -gt 0 ]
      do
      sleep 1
      counter=$(($counter - 1))
      done

      # now restart system
      reboot




      shutdown script reads:

      #!/bin/sh
      sleep 5
      shutdown now

      shutdown controller script reads:

      #!/bin/bash

      # check is a process is running, mono should be the HomeSeer process
      check_process() {
      # echo "$ts: checking $1"
      [ "$1" = "" ] && return 0
      [ `pgrep -n $1` ] && return 1 || return 0
      }

      # wait for the mono process to exit, HomeSeer will then be shut down
      while [ 1 ]; do
      check_process "mono"
      # echo "$?"
      if [ $? == 0 ]; then
      break
      fi
      sleep 2
      done

      # now power off the system
      poweroff
      I've got all these scripts in the base HomeSeer directory, but based on the behavior when selecting restart, or shutdown they are not working. How do I confirm that they are being called correctly? Why is this not working as expected on my own Linux install (works on my Z2 fine...) ?

      Comment


      • #4
        Check the permissions of the scripts - they need to have execute ('x') rights set.

        Is your HS3 application running as root? If not, you'll need to prepend the 'reboot', 'shutdown' and 'poweroff' commands with 'sudo', and ensure that the running user has sudo permissions.

        A brute-force way to see whats going on might be to add this line near the beginning (after the shebang line) of one or more of the scripts:

        Code:
        exec 2> /tmp/$(basename $0).log
        This will redirect any error output generated in the containing script to a file /tmp/script.log. If the script is called but no error messages are generated, there will be a zero-length /tmp/script.log created. If the script isn't getting called at all, or is failing to execute, no log file will be generated. After you modify the script, perform the calling action from the UI, then "ls -l /tmp" and see if a file appears and if it has any contents. Hopefully this will help narrow down the issue.

        Comment


        • #5
          Originally posted by zwolfpack View Post
          Check the permissions of the scripts - they need to have execute ('x') rights set.

          Is your HS3 application running as root? If not, you'll need to prepend the 'reboot', 'shutdown' and 'poweroff' commands with 'sudo', and ensure that the running user has sudo permissions.

          A brute-force way to see whats going on might be to add this line near the beginning (after the shebang line) of one or more of the scripts:

          Code:
          exec 2> /tmp/$(basename $0).log
          This will redirect any error output generated in the containing script to a file /tmp/script.log. If the script is called but no error messages are generated, there will be a zero-length /tmp/script.log created. If the script isn't getting called at all, or is failing to execute, no log file will be generated. After you modify the script, perform the calling action from the UI, then "ls -l /tmp" and see if a file appears and if it has any contents. Hopefully this will help narrow down the issue.
          Thanks - that helps. The log files tells me:

          Code:
          /usr/lib/HomeSeer/./restart.sh: line 22: reboot: command not found
          So 2 questions:
          1. Do I need to put in a full pathname for the reboot command, or something more 'findable' ?
          2. Why does the HS3 Linux install come with scripts that don't work?

          Thanks,

          Andrew

          Comment


          • #6
            It should be at /sbin/reboot. /sbin is normally in root's path, so I'm at a loss why its not found. What flavor Linux are you running? 'uname -a' will tell you that.

            Comment


            • #7
              Originally posted by zwolfpack View Post
              It should be at /sbin/reboot. /sbin is normally in root's path, so I'm at a loss why its not found. What flavor Linux are you running? 'uname -a' will tell you that.
              Linux Server 4.9.0-7-amd64 #1 SMP Debian 4.9.110-3+deb9u1 (2018-08-03) x86_64 GNU/Linux

              Easy enough to add the full path into this script. I'm starting HS3 through cron. Are there environment variables (like $PATH or such) that I should be setting somewhere to make this work every time (instead of provide full path name in each script) ?

              My cron for root is:

              Code:
              @reboot sleep 60 && exec sh /usr/lib/HomeSeer/autostart_hs > /tmp/hs3.log 2>&1
              and the autostart_hs script (untouched) is:

              Code:
              #!/bin/sh
              export LANG=en_US.UTF-8
              cd /usr/lib/HomeSeer
              mono HSConsole.exe

              Comment


              • #8
                PATH for cron jobs is very sparse. On Raspberry Pi (ARM Debian), it is "/usr/bin:/bin". According to this its hard-coded in the cron source. Further reading leads to suggestion to prepend "source /etc/profile" at start of cron commands.

                Comment


                • #9
                  Originally posted by zwolfpack View Post
                  PATH for cron jobs is very sparse. On Raspberry Pi (ARM Debian), it is "/usr/bin:/bin". According to this its hard-coded in the cron source. Further reading leads to suggestion to prepend "source /etc/profile" at start of cron commands.
                  Got it. So should I change my cron entry to this:
                  Code:
                  @reboot sleep 60 && source /etc/profile && exec sh /usr/lib/HomeSeer/autostart_hs > /tmp/hs3.log 2>&1
                  or would the extra '&&' not work ? Just trying to avoid creating a whole new script for startup of HS3...

                  Comment


                  • #10
                    That should work I think. Something I'd recommend for maximum portability is to change 'source' to single period '.'
                    'source' and '.' are synonyms in bash, but for the older /bin/sh, only '.' works. Just in case cron runs things under sh, I'd recommend
                    Code:
                    @reboot sleep 60 && . /etc/profile && exec sh /usr/lib/HomeSeer/autostart_hs > /tmp/hs3.log 2>&1
                    Note the space between the period and the command.

                    Another option is to add the
                    . /etc/profile
                    to the autostart_hs file. Note that this is running under /bin/sh, so 'source' won't work there.

                    Comment


                    • #11
                      Originally posted by zwolfpack View Post
                      That should work I think. Something I'd recommend for maximum portability is to change 'source' to single period '.'
                      'source' and '.' are synonyms in bash, but for the older /bin/sh, only '.' works. Just in case cron runs things under sh, I'd recommend
                      Code:
                      @reboot sleep 60 && . /etc/profile && exec sh /usr/lib/HomeSeer/autostart_hs > /tmp/hs3.log 2>&1
                      Note the space between the period and the command.

                      Another option is to add the
                      . /etc/profile
                      to the autostart_hs file. Note that this is running under /bin/sh, so 'source' won't work there.
                      Thanks zwolfpack. Will add the line to the top of the autostart_hs script file. Seems the neatest solution to me <numpty>...

                      Comment


                      • #12
                        I am still unable to make the menu commands - Tools/System/Shutdown ... work. (shutdown homeseer, shutdown system, and restart)
                        I am running HS3 Std on an Odroid HC1 and Ubuntu 18.04 LTS (headless) and Mono v5.0.1.1 (the latest that completely works with HomeSeer)
                        The systemd auto start process seems to be correct because HomeSeer starts and the log shows no errors

                        A copy of my files are listed below.
                        all three scripts are the same with the addition of:
                        trap poweroff EXIT in the shutdown_system script
                        trap shutdown -r now EXIT in the restart_system script
                        The exec 2> statement is meant to capture errors but the file is never created which tells me the script is never run?!?!

                        Any advice or help is greatly appreciated. I have been all over the forum looking for answers and these scripts are based on everything I've found.

                        auto start at boot via systemd = /etc/systemd/system/homeseer.service
                        ************************************************************ *********
                        [Unit]
                        Description=HomeSeer Service
                        After=network.target

                        [Service]
                        ExecStart=/usr/bin/mono /usr/local/HomeSeer/HSConsole.exe --log
                        # ExecStop=/usr/local/HomeSeer/stop_homeseer.sh
                        WorkingDirectory=/usr/local/HomeSeer
                        Restart=on-failure
                        RestartSec=10

                        [Install]
                        WantedBy=multi-user.target[Unit]
                        ************************************************************ **********

                        shutdown_homeseer.sh script locate in HomeSeer directory
                        ************************************************************ **********
                        #!/bin/sh
                        # hs3stop.sh - stop the HS3 application
                        # supports: systemd service shutdown

                        # login credentials used to login to web server; ignored if password not required
                        # login=usernameassword

                        # extract web server port from settings.ini and write log of errors

                        exec 2> /tmp/$(basename $0).log

                        hs3root=$(dirname $0) # where this script lives
                        webport=$(awk -F= '\
                        {
                        gsub("\015", "") # remove CR character
                        if ($1 == "gWebSvrPort") print $2
                        }
                        ' $hs3root/Config/settings.ini)

                        # send application shutdown command
                        for i in $(seq 1 5)
                        do
                        curl -s -o /dev/null ${login:+-u} $login -X POST -F 'scriptcmd=hs.shutdown()' -F 'runscript=Execute Command' "http:$
                        sleep 2
                        done

                        # delay until all mono processes terminate
                        mono=$(which mono) || exit
                        while pgrep -af $mono
                        do
                        sleep 5
                        do
                        ************************************************************ **********

                        shutdown_system.sh scripts located in HomeSeer directory
                        ************************************************************ **********
                        #!/bin/sh
                        # hs3stop.sh - stop the HS3 application
                        # supports: systemd service shutdown

                        # login credentials used to login to web server; ignored if password not required
                        # login=usernameassword

                        # extract web server port from settings.ini and write log of errors

                        exec 2> /tmp/$(basename $0).log
                        trap poweroff EXIT

                        hs3root=$(dirname $0) # where this script lives
                        webport=$(awk -F= '\
                        {
                        gsub("\015", "") # remove CR character
                        if ($1 == "gWebSvrPort") print $2
                        }
                        ' $hs3root/Config/settings.ini)

                        # send application shutdown command
                        for i in $(seq 1 5)
                        do
                        curl -s -o /dev/null ${login:+-u} $login -X POST -F 'scriptcmd=hs.shutdown()' -F 'runscript=Execute Command' "http:$
                        sleep 2
                        done

                        # delay until all mono processes terminate
                        mono=$(which mono) || exit
                        while pgrep -af $mono
                        do
                        sleep 5
                        done
                        ************************************************************ **********

                        restart_system.sh scripts located in HomeSeer directory
                        ************************************************************ **********
                        #!/bin/sh
                        # hs3stop.sh - stop the HS3 application
                        # supports: systemd service shutdown

                        # login credentials used to login to web server; ignored if password not required
                        # login=usernameassword

                        # extract web server port from settings.ini and write log of errors

                        exec 2> /tmp/$(basename $0).log
                        trap shutdown -r now EXIT

                        hs3root=$(dirname $0) # where this script lives
                        webport=$(awk -F= '\
                        {
                        gsub("\015", "") # remove CR character
                        if ($1 == "gWebSvrPort") print $2
                        }
                        ' $hs3root/Config/settings.ini)

                        # send application shutdown command
                        for i in $(seq 1 5)
                        do
                        curl -s -o /dev/null ${login:+-u} $login -X POST -F 'scriptcmd=hs.shutdown()' -F 'runscript=Execute Command' "http:$
                        sleep 2
                        done

                        # delay until all mono processes terminate

                        mono=$(which mono) || exit
                        while pgrep -af $mono
                        do
                        sleep 5
                        done
                        ************************************************************ **********

                        Comment


                        • #13
                          Restart System and Shutdown System break when HS3 is started via systemd. This can be corrected by modification to the systemd unit file to cleanly shutdown HS3, or by modifying the "restart.sh" and "shutdown_controller.sh" scripts.

                          It looks like you have some file naming and possibly some copy/paste issues.

                          For TOOLS->System->Restart System, the script needs to be named "restart.sh", not what you posted. It should contain something like this
                          Code:
                          #!/bin/bash
                          # restart.sh - HS3-linux tools script
                          # supports: TOOLS->System->Restart System
                          
                          mono=$(which mono) || exit
                          
                          trap /sbin/reboot EXIT
                          
                          while pgrep -af $mono.'*'\(HSConsole\|HomeSeer\)
                          do
                              sleep 2
                          done
                          Similarly, for TOOLS->System->Shutdown System, the script needs to be named "shutdown_controller.sh".
                          Code:
                          #!/bin/bash
                          # shutdown_controller.sh - HS3-linux tools script
                          # supports: TOOLS->System->Shutdown System
                          
                          mono=$(which mono) || exit
                          
                          trap /sbin/poweroff EXIT
                          
                          while pgrep -af $mono.'*'\(HSConsole\|HomeSeer\)
                          do
                              sleep 2
                          done
                          The hs3stop.sh script you posted (three times, with different names) is only applicable when called from the homeseer.service systemd unit file, which you aren't doing.

                          Comment


                          • #14
                            Thanks for the corrections - I will rename the files and go from there. They are not the same, there is a change in the trap command. However, due to trying to stop homeseer, stop the controller, or reboot the controller I assumed the process would be the same with the exception of the trap command.

                            Anuway ... thanks for the quick response. I will report back.

                            Comment


                            • #15
                              Again, thanks - the controller shutdown and restart are now working fine. I added the hs3stop.sh to the homeseer.service file as suggested also.
                              Greatly appreciated ...

                              Comment

                              Working...
                              X