2

A vision system program runs on a PC and I need it to be always running and on top, But sometimes there may happen a problem in the program and terminate it. So I need a script to check if the program is not running and run it as well.

I used cron job to the task, I wrote this cron job to run the script:

*/1 * * * * /home/masoud/Desktop/vision3/cron.sh

and the cron.sh is :

cd "${0%/*}"
if pgrep -x "video" > /dev/null
then
    echo "running"
else
    /home/masoud/Desktop/vision3/video &
fi

The cron.sh do the job correct, the cron job runs the script but it terminate it briefly. I can see my webcam LED turns on for a second. What am I doing wrong?

7
  • Some programs read from or write to the console, and this will fail when run in cron. At a quick thing, you may try changing the run line to be /home/masoud/Desktop/vision3/video >/dev/null </dev/null. – Brian C Jan 14 '17 at 9:26
  • @BrianC sorry but didn't help. – Masoud Jan 14 '17 at 9:32
  • If you manually run the script from the command line, does it work? Is it failing only within cron? – Brian C Jan 14 '17 at 9:35
  • @BrianC Yeah exactly. It is fine when I run the script from the command line. – Masoud Jan 14 '17 at 9:37
  • 1
    Is this an interactive program or a daemon? Does it have its own window? – Stephen Kitt Jan 14 '17 at 12:10
3

Cron jobs aren't really suited to managing desktop applications. You'd be better off starting the application from a looping shell script; at its simplest

#!/bin/sh
cd /home/masoud/Desktop/vision3
while :; do ./vision; done

That way whenever vision stops, it will be started again.

You may want to plan an "exit strategy" for when you really want to stop the program. This would do:

#!/bin/sh
cd /home/masoud/Desktop/vision3
while [ ! -f no_vision ]; do ./vision; done

Then when you want to stop the program,

touch /home/masoud/Desktop/vision3/no_vision

and close it — the shell script will stop too.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.