Announcement

Collapse
No announcement yet.

HomeSeer does not shutdown but restarts

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

    HomeSeer does not shutdown but restarts

    If I use the menu command Tools -> Linux -> Shutdown HomeSeer then I see the mono processes disappearing all right but the system is starting up right away.
    Is there a script problem in the root directory of HomeSeer?
    In the script 'shutdown_controller.sh' there is a 'poweroff' command on the last line, that is also not executed. Is there another script that does the job?
    ---
    Cor

    #2
    Originally posted by Cor View Post
    If I use the menu command Tools -> Linux -> Shutdown HomeSeer then I see the mono processes disappearing all right but the system is starting up right away.
    Is there a script problem in the root directory of HomeSeer?
    In the script 'shutdown_controller.sh' there is a 'poweroff' command on the last line, that is also not executed. Is there another script that does the job?
    ---
    Cor
    Post the script. I'd like to see what it does. Also, is HS called via systemd on startup? You could easily modify the script to do the following. Assuming it has the right access levels.

    #!/bin/sh
    service homeseer stop

    Comment


      #3
      This is the srcipt that is in homeseer root bin, but I am not shure if this is executed on the Tools -> Linux -> Shutdown HomeSeer command:

      Code:
      #!/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 am starting and shutting down homeseer with "sudo systemctl [start stop] homeseer".
      Everytime when homeseer starts up I get the message in the log that homeseer was not shutdown correctly. That is why I was looking how homeseer should be shutdown in a correct way. Now the stop homescript (stop_homeseer.sh) is:

      Code:
      #!/bin/bash
      cd /usr/local/HomeSeer
      /usr/bin/curl 'http://127.0.0.1:8080/LinuxTools' -H 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8' --data 'ConfirmShutdownhs=Yes' --compressed
      sleep 30s

      Comment


        #4
        I guess a reason for restarting HS is that the homeseer service is not stopped with the script that is called with the Tools -> Linux -> Shutdown HomeSeer command.
        ---
        Cor

        Comment


          #5
          Originally posted by Cor View Post
          I guess a reason for restarting HS is that the homeseer service is not stopped with the script that is called with the Tools -> Linux -> Shutdown HomeSeer command.
          ---
          Cor
          Exactly.

          You could give your user account sudo access to that one service. Instead of using "poweroff", you could change that out to the following. "service homeseer stop".

          Below is an example you will add to your /etc/sudoers file. You will need to double check the service name.

          Example:

          homeseer ALL=(ALL:ALL) ALL homeseer ALL=NOPASSWD:/usr/sbin/service homeseer

          Comment


            #6
            Well, if I change sodoers and the last line in the script then all mono processes are stopped except the HS process:

            /usr/bin/mono /usr/local/HomeSeer/HSConsole.exe --log

            So I suspect that this script is not used bij the drop down unix command, there mist be another script somewhere.

            Comment


              #7
              after a short period of time, all the mono processes start up again, because the service was not stopped. The message in the log about a not correct shutdown of homeseer has disappeared however. So I guess we are near to a solution. Why is the last line in the script not executed?

              Comment


                #8
                Originally posted by Cor View Post
                Well, if I change sodoers and the last line in the script then all mono processes are stopped except the HS process:

                /usr/bin/mono /usr/local/HomeSeer/HSConsole.exe --log

                So I suspect that this script is not used bij the drop down unix command, there mist be another script somewhere.
                Everything that is associated with shutting down the service will be taken down. I assume that includes any mono related processes.

                just do a service Homeseer stop from cli. Then do a top | grep -I mono and see what’s still running.

                Comment


                  #9
                  If I do a 'sudo service homeseer stop' then all mono processes are stopped, inclusive the console.exe from HS.

                  Comment


                    #10
                    Originally posted by Cor View Post
                    If I do a 'sudo service homeseer stop' then all mono processes are stopped, inclusive the console.exe from HS.
                    Maybe I’m missing something. Do you Not want the script to shut things down?

                    Comment


                      #11
                      I want the script to end all processes related to HS, so all mono processes.

                      If I use the method with the 'systemctl stop homeseer' from cli then all mono processes are stopped indeed, but I always see in the log that homeseer was not successfully shutdown the last time. This is the reason that I try to find out how within the GUI of HS this shutdown is performed.

                      So I can learn what is going wrong with the method I normally use and learn what has to be changed.

                      Comment


                        #12
                        mcsMQTT, with its internal broker, monitors a wrap around heartbeat and will restart it if missing. The monitoring is stopped when HS calls the plugin's ShutdownIO method that should happen when HS shuts down. Even the monitoring were to continue it is no reason that HS should be restarting.

                        Many months ago I provided feedback to Rich that his method to handle shutdown failed because he was monitoring the Mono process presence. This assumed that only HS was using Mono and I have other .NET/Mono applications running on the same computer. He did provide feedback that he changed the technique.

                        What is the relationship between mcsMQTT and the restart issue? Does it only happen when mcsMQTT is running?

                        Comment


                          #13
                          I disabled mcsMQTT and did a Tools -> Linux -> Shutdown HomeSeer. I see all the mono processes closing one by one and then HS is starting again. So it also happens if mcsMQTT is not running.
                          ---
                          Cor

                          Comment


                            #14
                            Perhaps I should post this to the HS4 Testing (Beta) thread.
                            ---
                            Cor

                            Comment


                              #15
                              What does your systemd unit file look like? Most of the ones I've seen posted don't adequately address service shutdown.

                              HS doesn't act as a proper daemon process. A proper daemon traps the SIGTERM signal and performs an orderly shutdown when received. HS doesn't, so receipt of SIGTERM produces an exception and immediate abort. That's why you get the 'not shutdown properly' message on restart.

                              The reason your poweroff command isn't executed is that on service shutdown, SIGTERM is sent to child processes as well, including the shutdown_controller.sh script. So it never gets to the end.

                              A work around is to add the 'KillMode=none' directive to your unit file, coupled with a 'ExecStop' directive which specifies a script that will cause HS to perform an orderly shutdown.

                              KillMode=none prevents any signals from being issued, so the script is necessary to stop the processes.

                              The archive posted here contains a unit file 'homeseer.service' and a shutdown script 'hsstop.sh'. The latter goes in the HS root folder.

                              Also included is a replacement shutdown_controller.sh which will execute the poweroff even if it gets a SIGTERM.

                              homeseer.service
                              Code:
                              [Unit]
                              Description=HomeSeer Home Automation
                              Documentation=https://homeseer.com/support-home/
                              After=network-online.target remote-fs.target time-sync.target
                              Before=multi-user.target
                              
                              [Service]
                              WorkingDirectory=/usr/local/HomeSeer
                              ExecStart=/usr/bin/mono HSConsole.exe --log
                              SyslogIdentifier=HomeSeer HSx
                              StandardOutput=null
                              Restart=on-failure
                              RestartSec=30
                              KillMode=none
                              TimeoutStopSec=300
                              ExecStop=/usr/local/HomeSeer/hsstop.sh
                              
                              [Install]
                              WantedBy=multi-user.target
                              hsstop.sh
                              Code:
                              #!/bin/bash
                              # hsstop.sh - stop the HS application
                              # supports: systemd service shutdown
                              
                              # import login credentials used to login to web server
                              # these are ignored if password not required
                              inifile=$(dirname $0)/Config/$(basename $0 .sh).ini
                              login=
                              test -r $inifile && . $inifile
                              
                              # extract web server port from settings.ini
                              hsroot=$(dirname $0)  # where this script lives
                              webport=$(awk -F= '\
                              {
                                  gsub("\015", "") # remove CR character
                                  if ($1 == "gWebSvrPort") print $2
                              }
                              ' $hsroot/Config/settings.ini)
                              
                              # send application shutdown command
                              for i in $(seq 1 5)
                              do
                                  curl -f -s -o /dev/null ${login:+-u} $login --data 'ConfirmShutdownhs=Yes' "http://localhost:$webport/LinuxTools"
                                  rc=$?
                                  test $rc -eq 0 && break
                                  sleep 2
                              done
                              
                              killmain()
                              {
                                  test -n "$MAINPID" && kill -0 $MAINPID && kill $MAINPID
                              }
                              
                              trap killmain EXIT
                              
                              # if curl cmd unsuccessful, terminate main process
                              test $rc -ne 0 && killmain
                              
                              # wait until all HomeSeer mono processes terminate, with timeout
                              maxwait=300
                              polltime=5
                              mono=$(which mono) || exit
                              for (( t=0; t<$maxwait; t+=$polltime ))
                              do
                                  pgrep -af $mono.'*'\(HSConsole\|HomeSeer\) || break
                                  sleep $polltime
                              done
                              shutdown_controller.sh
                              Code:
                              #!/bin/bash
                              # shutdown_controller.sh - HSx-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

                              Comment

                              Working...
                              X