erondem
Posts: 23
Joined: Wed Jul 11, 2018 12:19 pm

Re start a program after it crashes

Thu Nov 29, 2018 11:36 am

In my pi there is a progam written by me and it crashes if internet goes for long time. I need to re login via ssh and start again the program.
What I need is when program.exe stopped working it needs to be re-started.



I added both below lines to one by one to ~/.config/lxsession/LXDE-pi/autostart but it didn't help

Code: Select all

sudo /home/pi/Desktop/program.exe

Code: Select all

cd /home/pi/Desktop; ./program.exe
here is how my autostart looks like

Code: Select all

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
@cd /home/pi/Desktop;  program.exe


Do you guys have any idea? Thanks in advance

Ps: it is not a gui program
Last edited by erondem on Thu Nov 29, 2018 11:51 am, edited 1 time in total.

User avatar
RaTTuS
Posts: 10563
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Re start a program after it crashes

Thu Nov 29, 2018 11:40 am

yes use a wrapper script

not tested :-

Code: Select all

#!/bin/bash
while :
do 
  program.exe
done
then run that script instead of your program

or write your code better


N.B.
why call it .exe - thats not the linux / unix way
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

erondem
Posts: 23
Joined: Wed Jul 11, 2018 12:19 pm

Re: Re start a program after it crashes

Thu Nov 29, 2018 11:49 am

RaTTuS wrote:
Thu Nov 29, 2018 11:40 am
yes use a wrapper script

not tested :-

Code: Select all

#!/bin/bash
while :
do 
  program.exe
done
then run that script instead of your program

or write your code better


N.B.
why call it .exe - thats not the linux / unix way

But how does tihs script understand if program.p(lets call it .p instead of .exe thanks for this) running at that time?

MarkDH102
Posts: 404
Joined: Fri Feb 13, 2015 3:18 pm

Re: Re start a program after it crashes

Thu Nov 29, 2018 2:17 pm

My advice is to look at the error that is being generated (a http request timeout?) and trap it WITHIN your program. Enclose the call that is crashing within a Try..Catch block (language dependent of course). This is the RIGHT way to tackle such things.
Here is an example of some Python code I use.

Code: Select all

try :

    page = requests.get('http://www.worcesternews.co.uk/li/traffic_and_travel.in.Worcester', timeout = 15.0)
except (TimeoutException, requests.ConnectionError, requests.Timeout, requests.RequestException):
    # YELLOW LED on
    GPIO.output(CONST.INTERNET_ACCESS_LED, 0)
    # Presumably , no internet connection...
    key.addstr(15, 0, "Error (Timeout)")
except :
    e = sys.exc_info()[0]
    key.addstr(15, 0, "Error (Unknown) {}".format(e))
finally:
   # Kill the timeout...
My program runs in a console window and writes to the console using the "curses" library - key.addstr.

User avatar
RaTTuS
Posts: 10563
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Re start a program after it crashes

Thu Nov 29, 2018 2:35 pm

erondem wrote:
Thu Nov 29, 2018 11:49 am
RaTTuS wrote:
Thu Nov 29, 2018 11:40 am
yes use a wrapper script

not tested :-

Code: Select all

#!/bin/bash
while :
do 
  program.exe
done
then run that script instead of your program

or write your code better


N.B.
why call it .exe - thats not the linux / unix way

But how does tihs script understand if program.p(lets call it .p instead of .exe thanks for this) running at that time?
it does not know if it's already running
but when program exits it will just loop round and run it again [either exiting normally or errors] - this will not help if the program crashes as a zombie but that's another issue
...
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

Return to “Beginners”