User Tools

Site Tools


Sidebar

Index

software:motion-watchdog-to-fix-daemon-shutting-down

Motion watchdog to fix daemon shutting down

If you are using Motion for your security cameras, you may have noticed that sometimes the daemon shut down unexpectedly for a reason or another. E.g., I've noticed that sometimes it stops when the fps of one camera are too low (don't ask me why that triggers a crash of the daemon).

Whatever the case, it could happen at any time for any reason, and since this is a kind of mission critical software it's always a good idea to have a watchdog to restart the service whatever the case.

#!/bin/bash
# MOTION WATCHDOG - restart the service if stopped
# Call this script from root crontab
 
SERVICE="motion"
# Where to write the log...
LOG='/home/john/security/watch.log'
# ... and the user that can manage the log without sudo
OWNER='john'
 
if ! pidof "$SERVICE" >/dev/null
then
   #echo "$SERVICE stopped"
   #restart the service
   if [ ! -f "$LOG" ]; then
      touch "$LOG"
      chown "$OWNER" "$LOG"
   fi
 
   echo $(date +'%F %T') 'Restart' >> "$LOG"
   motion -c /etc/motion/motion.conf
fi

Call this script from the Linux cron every minute and you are done. The watch.log will show when and how many times the daemon has shut down and restarted by this script.

software/motion-watchdog-to-fix-daemon-shutting-down.txt · Last modified: 2020/01/31 08:18 by rik