Setting up the raspberry pi watchdog

From Domoticz
Jump to: navigation, search


This guide describes how to install the hardware watchdog, a system that knows when your raspberry is down, and will automatically reboot it in case this happens.

This guide will also work for the cubieboard/truck, if you are using the firmware mentioned on this wiki,
the watchdog is already installed and you can continue to Install the software watchdog daemon

Install watchdog

sudo modprobe bcm2708_wdog
echo "bcm2708_wdog" | sudo tee -a /etc/modules

Install the software watchdog daemon

sudo apt-get install watchdog
sudo update-rc.d watchdog defaults

Configure the watchdog daemon

Open /etc/watchdog.conf with your favorite editor (mine is nano).

sudo nano /etc/watchdog.conf

Uncomment the line that starts with #watchdog-device by removing the hash (#) to enable the watchdog daemon to use the watchdog device. Uncomment the line that says #max-load-1 = 24 by removing the hash symbol to reboot the device if the load goes over 24 over 1 minute. A load of 25 of one minute means that you would have needed 25 Raspberry Pis to complete that task in 1 minute. You may tweak this value to your liking.

In some cases you should also add the following line to allow watchdog even to start-up:

watchdog-timeout = 15

The timeout defaults to 60 seconds, but this results sometimes in not starting up at all (probably a watchdog bug). Do not set this value lower than 15.

If you only want to setup the watchdog to restart in case your system hangs, continue to the section Start the watchdog daemon

Use watchdog to check Domoticz

This script relies on the fact that the Domoticz.log is written in /tmp/ newer installs have this option disabled by default. To make this script work you must enable the logfile.

sudo nano /etc/init.d/

and change the line OPTIONS="-www 8080" to:

OPTIONS="-www 8080 -loglevel=1 -log /tmp/domoticz.log"

(* Alternative ) In my case, the option must be set up like this

DAEMON_ARGS="$DAEMON_ARGS -www 8080 -loglevel=1"
DAEMON_ARGS="$DAEMON_ARGS -log /tmp/domoticz.log"

Setup mailer:

sudo apt-get install sendmail-bin
sudo apt-get install mutt

If mutt does not install, then try updating your pi first by running:

sudo apt-get update

Then continue to:

cd /home/pi
create the file .muttrc with the following content: (vi .muttrc or nano .muttrc)
# basic .muttrc for use with Gmail
# Change the following six lines to match your Gmail account details
set imap_user = "[email protected]"
set imap_pass = ""
set smtp_url = "smtp://[email protected]:587/"
set smtp_pass = ""
set from = "[email protected]"
set realname = "Firstname Lastname"
# # Change the following line to a different editor you prefer.
set editor = 'vim + -c "set textwidth=72" -c "set wrap"'
# Basic config
set folder = "imaps://"
set spoolfile = "+INBOX"
set imap_check_subscribed=yes
set hostname =
set mail_check = 120
set timeout = 300
set imap_keepalive = 300
set postponed = "+[GMail]/Drafts"
set header_cache=~/.mutt/cache/headers
set message_cachedir=~/.mutt/cache/bodies
set certificate_file=~/.mutt/certificates
set move = no
set include
set sort = 'threads'
set sort_aux = 'reverse-last-date-received'
set auto_tag = yes
set pager_index_lines = 10
ignore "Authentication-Results:"
ignore "DomainKey-Signature:"
ignore "DKIM-Signature:"
hdr_order Date From To Cc
alternative_order text/plain text/html *
auto_view text/html
bind editor <Tab> complete-query
bind editor ^T complete
bind editor <space> noop
# # Gmail-style keyboard shortcuts
macro index,pager am "<enter-command>unset trash\n <delete-message>" "Gmail archive message" # different from Gmail, but wanted to keep "y" to show folders.
macro index,pager d "<enter-command>set trash=\"imaps://[GMail]/Bin\"\n <delete-message>" "Gmail delete message"
macro index,pager gi "<change-folder>=INBOX<enter>" "Go to inbox"
macro index,pager ga "<change-folder>=[Gmail]/All Mail<enter>" "Go to all mail"
macro index,pager gs "<change-folder>=[Gmail]/Starred<enter>" "Go to starred messages"
macro index,pager gd "<change-folder>=[Gmail]/Drafts<enter>" "Go to drafts"
macro index,pager gl "<change-folder>?" "Go to 'Label'" # will take you to a list of all your Labels (similar to viewing folders).

edit the first lines to setup your gmail account info

Create a mail script in /home/pi:

cd /home/pi/
sudo nano mailIP

Paste the following (repalce YOURMAIL with full email address of where you want the email sent i.e. [email protected]):

 #my_ip=`ifconfig | grep 'inet addr:'| grep -v '' | cut -d: -f2 | awk '{print $1}'`
 my_ip=`wget -q -O -|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'`
 my_pi="Domoticz RaspberryPi has rebooted! "
 message="Your Pi has rebooted at $today. Current IP address = $my_ip"
 echo $message > message.txt
 mutt -s "${my_pi}" ${mailreceiver} < message.txt && rm message.txt

The commented line fetches the internal ip, example fetches external ip.

Make sure the mailIP can be executed:

sudo chmod +x mailIP

Test the script by running it:


Setup rc.local to send the mail at each reboot: Edit /etc/rc.local:

sudo nano /etc/rc.local

Add this above the exit 0 line:

sudo /home/pi/mailIP & 

Make sure the rc.local can be executed:

sudo chmod 0755 /etc/rc.local
sudo bash
cp /home/pi/.muttrc /root

sudo nano /etc/watchdog.conf

Uncomment the lines file and change and set them to:

file                    = /tmp/domoticz.log
change                  = 300

This setting will restart the pi when domoticz.log hasn't been updated in 5 minutes, the time is in seconds (tested).

Be sure to remember this when stopping the domoticz service by hand!

and for new system , that do nothing yet, you can prevent unwanted reboot by adding a script like this :

cd /home/pi/domoticz/scripts/lua
nano script_time_domotizAlive.lua
commandArray = {}
print('Domoticz alive !')
return commandArray

It will update the log every minute.

Start the watchdog daemon

sudo service watchdog start

Source: [1] Source: [2]