Script to restart Domoticz if it crashes Topic is solved

All kinds of 'OS' scripts

Moderator: leecollings

jannl
Posts: 625
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Script to restart Domoticz if it crashes

Post by jannl »

I test if domoticz is running by issuing a json call. The status of the service might be ok even if domoticz hangs.
adjego
Posts: 11
Joined: Monday 16 February 2015 11:42
Target OS: Raspberry Pi / ODroid
Domoticz version: V3.5163
Location: Eindhoven
Contact:

Re: Script to restart Domoticz if it crashes

Post by adjego »

I also have this happening.

Jan 3 23:22:00 raspberrypi domoticz: Error: /home/pi/domoticz/domoticz() [0x1a9650]
Jan 3 23:22:00 raspberrypi domoticz: Error: /home/pi/domoticz/domoticz(_Z14signal_handleri+0x58) [0x1a9708]
Jan 3 23:22:00 raspberrypi domoticz: Error: /lib/arm-linux-gnueabihf/libc.so.6(__default_sa_restorer_v2+0) [0x76b9eb20]
Jan 3 23:22:00 raspberrypi domoticz: Error: /lib/arm-linux-gnueabihf/libc.so.6(gsignal+0x40) [0x76b9d8dc]

Is there an actual fix for this issue?
I now have the script in place, but it's a workaround, not a fix.

@jannl, can you share your script?
jannl
Posts: 625
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Script to restart Domoticz if it crashes

Post by jannl »

Basically the script I use is in the wiki:

https://www.domoticz.com/wiki/Monitoring_domoticz

I only tweaked the curl parameters a little bit to include a timeout
spikehome
Posts: 24
Joined: Friday 04 March 2016 15:13
Target OS: -
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by spikehome »

tjabas wrote:Im really new to this Linux and rasberry, but how do i install this script?

I have lot of problems with Domoticz freezing, so i guess this script would do the trick.
I had the problem also.
Installed new and restored the db.
Now it is working fine for a week (normaly in a few days web and ssh not accessible).
User avatar
philchillbill
Posts: 396
Joined: Monday 12 September 2016 13:47
Target OS: Linux
Domoticz version: beta
Location: Eindhoven. NL
Contact:

Re: Script to restart Domoticz if it crashes

Post by philchillbill »

The script works by checking the text that results from running the command: sudo service domoticz.sh status
The rpi version of Linux replies with a slightly different text that e.g. Ubuntu: 'domoticz is running' vs 'active (running)'.
If the version of the script you are using is checking for the wrong text it will never find it. Please look carefully at the earlier examples above and watch out for these differences I just mentioned.
Alexa skills author: EvoControl, Statereport, MediaServer, LMS-lite
hansrune
Posts: 49
Joined: Monday 07 October 2013 10:51
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by hansrune »

I use another approach, i.e a healthcheck script that runs from cron to check on port listeners, network connectivity and more.
Reboots on some conditions, restarts Domoticz if no listener for that network port. I chose to not restart services if someone is logged in to a shell, assuming that I me doing some maintenance.

You can pick this up, or perhaps use some ideas from https://github.com/hansrune/domoticz-co ... lthchkdomo

Instruction in the beginning....
User avatar
K3rryBlue
Posts: 77
Joined: Tuesday 07 February 2017 22:13
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.10xx
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by K3rryBlue »

Made a small change to this script. As the status response changed:

Code: Select all

#!/bin/bash
  DomoticzState=`sudo service domoticz.sh status`
   if [[ $DomoticzState == *"active (running)"* ]]
          then
                  echo 'Domoticz is running. Nothing to do.'
  elif [[ $DomoticzState == *"inactive (dead)"* ]]
          then
                  echo 'Domoticz is not running. Restarting Domoticz...'
                  sudo service domoticz.sh restart
                  echo 'Domoticz restarted.'
  fi
Domoticz V4.10717, RPi2 -> Linux raspberrypi 4.4.47-v7+ , RFXCOM - RFXtrx433 USB 433.92MHz ,RAZBERRY2 and OpenZWave USB Version: 1.4-2363-g1f10253-dirty
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Script to restart Domoticz if it crashes

Post by Egregius »

I believe it's better to query the json API to check that domoticz is online.
Because:
a) A service might be running but hanging and thus give a false response
b) A port might be listening but the service behind it hangs, again false response.
oohlaf
Posts: 21
Joined: Monday 06 February 2017 0:57
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by oohlaf »

If it's just to check when domoticz has crashed (ie exited) and if your system uses systemd, then use the following:
(change the execstart parameters to your own usage)

Code: Select all

[Unit]
Description=Domoticz Home Automation
After=network.target

[Service]
Type=forking
User=pi
Group=pi
WorkingDirectory=/home/pi/domoticz
PIDFile=/var/run/domoticz/domoticz.pid
ExecStart=/home/pi/domoticz/domoticz -daemon -pidfile /var/run/domoticz/domoticz.pid -www 8080 -sslwww 0 -log /var/log/domoticz/domoticz.log
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=10

[Install]
WantedBy=multi-user.target
Alias=domoticz.sh.service
User avatar
K3rryBlue
Posts: 77
Joined: Tuesday 07 February 2017 22:13
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.10xx
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by K3rryBlue »

Yes, there are indeed better, or multiple, ways but so far the simple script does what it needs to do.
However i must admit, that lately, the pi runs very stable, by using berryboot and run everything from a USB thumb. teh sd-card is only used to boot the rPi. So far Domoticz is stable for 2 weeks, no "database malfunctions" or crashes.

Cause the rPi runs stable now, it revealed a new problem, with OpenZWave + RazBerry2. Need to figure a way to detect that ZWave is not responding anymore, probably via a JSON call. IF no response THEN reboot rPi.
Domoticz V4.10717, RPi2 -> Linux raspberrypi 4.4.47-v7+ , RFXCOM - RFXtrx433 USB 433.92MHz ,RAZBERRY2 and OpenZWave USB Version: 1.4-2363-g1f10253-dirty
uc530
Posts: 6
Joined: Tuesday 07 March 2017 0:27
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by uc530 »

Hello!
I am new user of raspberry and domoticz and I am asking you for some help.
I have no success with the script to make it run. I made a file with name domoticz_state_checker.sh with:

Code: Select all

#!/bin/bash
while true
do

DomoticzState=`sudo service domoticz status`

if [ $DomoticzState == "active (running)" ]
        then
                echo 'Domoticz is running. Nothing to do.'
elif [ $DomoticzState == "inactive (dead)" ]
        then
                echo 'Domoticz is not running. Restarting Domoticz...'
                sudo service domoticz restart
                echo 'Domoticz restarted.'
         fi

sleep 15
done
But I have all possible errors listed when I try to run it:

Code: Select all

sudo su pi /home/pi/domoticz/scripts/domoticz_state_checker.sh
/home/pi/domoticz/scripts/domoticz_state_checker.sh: line 10: syntax error near unexpected token `elif'
'home/pi/domoticz/scripts/domoticz_state_checker.sh: line 10: `elif [ $DomoticzState == "inactive (dead)" ]
If I change elif with elseif, then I have:

Code: Select all

sudo su pi /home/pi/domoticz/scripts/domoticz_state_checker.sh
/home/pi/domoticz/scripts/domoticz_state_checker.sh: line 18: syntax error near unexpected token `done'
/home/pi/domoticz/scripts/domoticz_state_checker.sh: line 18: `done'
Can anyone please write it from the beginning, how to make this script to work properly. Where to put the files, how to write the code correctly...
I run raspbyan jessie on RPI ver.1.

Thanks in advance!
Ps:Sorry for my english.
User avatar
K3rryBlue
Posts: 77
Joined: Tuesday 07 February 2017 22:13
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.10xx
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by K3rryBlue »

You ran a bit short on [ and ]
Please use this:

Code: Select all

#!/bin/bash
while true
do

DomoticzState=`sudo service domoticz status`

if [[ $DomoticzState == *"active (running)"* ]]
        then
                echo 'Domoticz is running. Nothing to do.'
elif [[ $DomoticzState == *"inactive (dead)"* ]]
        then
                echo 'Domoticz is not running. Restarting Domoticz...'
                sudo service domoticz restart
                echo 'Domoticz restarted.'
         fi

sleep 15
done
To check this every 15 seconds might be an overkill.......
Domoticz V4.10717, RPi2 -> Linux raspberrypi 4.4.47-v7+ , RFXCOM - RFXtrx433 USB 433.92MHz ,RAZBERRY2 and OpenZWave USB Version: 1.4-2363-g1f10253-dirty
uc530
Posts: 6
Joined: Tuesday 07 March 2017 0:27
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by uc530 »

Hi!
Thanks for the answer.

I tryed that alredy with the code you posted few days ago, but i`ve got errors:

Code: Select all

pi@raspberrypi:~ $ sudo su pi /home/pi/domoticz/scripts/domoticz_state_checker.sh
/home/pi/domoticz/scripts/domoticz_state_checker.sh: line 7: syntax error in conditional expression
'home/pi/domoticz/scripts/domoticz_state_checker.sh: line 7: syntax error near `]]
/home/pi/domoticz/scripts/domoticz_state_checker.sh: line 7: `if [[ $DomoticzState == *"active (runn'ng)"* ]]
About the 15 seconds.... I`ll change that with longer period.
User avatar
K3rryBlue
Posts: 77
Joined: Tuesday 07 February 2017 22:13
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.10xx
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by K3rryBlue »

I think you are creating your files in windows and copy them to the rPi. :shock:
  • Please copy the code below to your script file. I use as example "domoticz_status_check.sh"

    Code: Select all

    #!/bin/bash
    while true
    do
    
    DomoticzState=`sudo service domoticz.sh status`
    
    if [[ $DomoticzState == *"active (running)"* ]]
            then
                    echo 'Domoticz is running. Nothing to do.'
    elif [[ $DomoticzState == *"inactive (dead)"* ]]
            then
                    echo 'Domoticz is not running. Restarting Domoticz...'
                    sudo service domoticz.sh restart
                    echo 'Domoticz restarted.'
    fi
    
    sleep 15
    done
    
  • Save the file and run the following commands via putty"

    Code: Select all

    sudo chmod 755 domoticz_status_check.sh
  • Remove the horror from windows

    Code: Select all

    sed -i -e 's/\r$//' domoticz_status_check.sh
  • Execute the script

    Code: Select all

    sudo ./domoticz_status_check.sh
The script should run now
Domoticz V4.10717, RPi2 -> Linux raspberrypi 4.4.47-v7+ , RFXCOM - RFXtrx433 USB 433.92MHz ,RAZBERRY2 and OpenZWave USB Version: 1.4-2363-g1f10253-dirty
uc530
Posts: 6
Joined: Tuesday 07 March 2017 0:27
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by uc530 »

Thank you very much! It works!
Another one thing please... Procedure how to make the script to run after rpi reboot.
Thanks again! I owe you a big beer!
Last edited by uc530 on Thursday 09 March 2017 0:15, edited 1 time in total.
User avatar
K3rryBlue
Posts: 77
Joined: Tuesday 07 February 2017 22:13
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.10xx
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by K3rryBlue »

Domoticz V4.10717, RPi2 -> Linux raspberrypi 4.4.47-v7+ , RFXCOM - RFXtrx433 USB 433.92MHz ,RAZBERRY2 and OpenZWave USB Version: 1.4-2363-g1f10253-dirty
uc530
Posts: 6
Joined: Tuesday 07 March 2017 0:27
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Script to restart Domoticz if it crashes

Post by uc530 »

Hi!
Script without loop and crontab command works like charm.
Thanks again for help.
script and added all rights:

Code: Select all

#!/bin/bash

DomoticzState=`sudo service domoticz.sh status`

if [[ $DomoticzState == *"active (running)"* ]]
        then
                echo 'Domoticz is running. Nothing to do.'
elif [[ $DomoticzState == *"inactive (dead)"* ]]
        then
                echo 'Domoticz is not running. Restarting Domoticz...'
                sudo service domoticz.sh restart
                echo 'Domoticz restarted.'
fi


crontab edit:

Code: Select all

crontab -e
*/1 * * * * /home/pi/domoticz/scripts/domoticz_state_checker.sh
With best regards UC530
User avatar
K3rryBlue
Posts: 77
Joined: Tuesday 07 February 2017 22:13
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.10xx
Location: Netherlands
Contact:

Re: Script to restart Domoticz if it crashes

Post by K3rryBlue »

Thanks for your feedback. To make it even more complete you could add a simple log to your script, so you know when something happened.
If Domoticz need to restart it will write a datetime stamp to a file.

Code: Select all

#!/bin/bash
  CONFIG=/home/pi/domoticz/scripts/domoticz_state_checker.txt
  dt=$(date '+%d/%m/%Y %H:%M:%S')
  DomoticzState=`sudo service domoticz.sh status`
   if [[ $DomoticzState == *"active (running)"* ]]
          then
				  echo "Domoticz is running. Nothing to do."
  elif [[ $DomoticzState == *"inactive (dead)"* ]]
          then
		  echo "$dt, Domoticz is not running. Restarting Domoticz..." >> "$CONFIG"
                  sudo service domoticz.sh restart
                  echo "$dt, Domoticz restarted..." >> "$CONFIG"
  fi
Domoticz V4.10717, RPi2 -> Linux raspberrypi 4.4.47-v7+ , RFXCOM - RFXtrx433 USB 433.92MHz ,RAZBERRY2 and OpenZWave USB Version: 1.4-2363-g1f10253-dirty
jannl
Posts: 625
Joined: Thursday 02 October 2014 6:36
Target OS: Raspberry Pi / ODroid
Domoticz version: 2022.2
Location: Geleen
Contact:

Re: Script to restart Domoticz if it crashes

Post by jannl »

I get a pushover notification when domoticz is restarted. I only check is json is respondig. With the status check you propably won't detect if domoticz hangs.
User avatar
havnegata
Posts: 114
Joined: Wednesday 10 September 2014 11:05
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.10162
Location: Norway
Contact:

Re: Script to restart Domoticz if it crashes

Post by havnegata »

jannl wrote:I get a pushover notification when domoticz is restarted. I only check is json is respondig. With the status check you propably won't detect if domoticz hangs.
This is what I would like as well :D Do you care to describe a bit closer how you achieve this?
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests