In this short post I would like to just write some useful notes I collected and tested of how to make sure a program keeps on running.
I had this kind of ‘problem’ with my home-surveillance, which seemed to ‘crash’ at some occasions.
So besides of course trying to solve the actual problem, I thought it would be a good thing to have another routine that checks if this routine is still running and if not re-starts it.
This is actually not very complicated and can be solved like this:
- Write a linux shell script that checks if the process or service (in this example the service with the name ‘displayMotion’) is running and if not starts it. I am calling this shell script “isProcessRunning.sh”:
isProcessRunning -- Shell script to restart a routine or serviceShell12345678#!/bin/bashservice="displayMotion"if ! ps -e | grep "$service" ; then# echo "not running -- restarting "/home/pi/camClass/VideoCapture_version/displayMotionfi
Note: Make sure to use absolute paths. Absolute paths should not only be used in the shell script (isProcessRunning.sh) but also in the routine or script the shell script is calling (here: displayMotion)
- Next schedule a regular executions of the shell script isProcessRunning.sh, which in turn checks if your actual service (displayMotion) is still running. This can be easily done adding following call in crontab. Open crontab with following call:
open the crontabShell1> sudo crontab -e:
and add following line:
crontab -- schedule checks if process is runningShell1*/5 8-19 * * MON,TUE,WED,THU,FRI /home/pi/camClass/VideoCapture_version/isProcessRunning.sh
Note: Also here make sure to use absolute path.
This will check on the 5 workdays of the week between 8AM and 7PM every 5 minutes if the motion detection algorithm is running. If it is not running the shell script will re-start it.
Additionally it helped me to do following:
- In the C++ program, when saving the video to a file do not use “cout” or open any windows
- In C++ use absolute paths such as:
strftime(detectionfile, 80, “/home/pi/camClass/VideoCapture_version/detections/%d%h%Y_%H%M%S.avi”, timeinfo);
Looking forward to your feedback…