OF on raspberry PI 4 with buster, how to restart crashed app?

I have successfully managed to get my OF app to auto start once the RPI desktop is done starting.
To make this happen I had to:

sudo nano /etc/xdg/lxsession/LXDE-pi/autostart

then add this line to the file

@sudo /home/pi/openFrameworks/apps/folderName1/folderName2/bin/myApp

Now I was hoping to find a way to use systemd to tread the app as a service and start it when it is not running. Maybe it’s a bad idea to run it as a service and there might be a completely different way to achieve this. ?

I tried following these steps:
https://slippytrumpet.io/posts/running-your-app-as-service-on-pi-zero-w/
and these:


Also this video was helpful:

But it still does not work.
Here my print out after calling sudo service myProg status

● myProg.service - my service
   Loaded: loaded (/etc/systemd/system/myProg.service; disabled; vendor preset: enabled)
   Active: failed (Result: signal) since Wed 2020-01-22 15:06:10 EST; 6s ago
  Process: 1079 ExecStart=/usr/bin/sudo /home/pi/openFrameworks/apps/remotePulse/remotePulse/bin/remotePulse (code=k
 Main PID: 1079 (code=killed, signal=SEGV)

Jan 22 15:06:10 raspberrypi systemd[1]: Started my service.
Jan 22 15:06:10 raspberrypi sudo[1079]:     root : TTY=unknown ; PWD=/home/pi/openFrameworks/apps/remotePulse/remote
Jan 22 15:06:10 raspberrypi sudo[1079]: pam_unix(sudo:session): session opened for user root by (uid=0)
Jan 22 15:06:10 raspberrypi sudo[1079]: [ error ] ofAppGLFWWindow: 65544: X11: The DISPLAY environment variable is m
Jan 22 15:06:10 raspberrypi sudo[1079]: [ error ] ofAppGLFWWindow: couldn't init GLFW
Jan 22 15:06:10 raspberrypi sudo[1079]: [ error ] ofAppGLFWWindow: 65537: The GLFW library is not initialized
Jan 22 15:06:10 raspberrypi sudo[1079]: pam_unix(sudo:session): session closed for user root
Jan 22 15:06:10 raspberrypi systemd[1]: myProg.service: Main process exited, code=killed, status=11/SEGV
Jan 22 15:06:10 raspberrypi systemd[1]: myProg.service: Failed with result 'signal'.

Here my myProg.service file:

[Unit]
Description=my service
After=network.target

[Service]
User=root
WorkingDirectory=/home/pi/openFrameworks/apps/remotePulse/remotePulse/bin
ExecStart=sudo /home/pi/openFrameworks/apps/remotePulse/remotePulse/bin/remoteP$

[Install]
WantedBy=multi-user.target

You’ll probably be able to do it with Linux scripting, which I can’t help much with, but is ofxWatchdog (https://github.com/toolbits/ofxWatchdog) an option?

Hi,
I’ve recently used systemd to start some SuperCollider programs and had accidentally set Restart=always, which at least always restarted SC when I killed it with kill -9 PID.
Maybe you could try to start the app with systemd, instead of the autostart file?
I simply followed the Raspberry Pi instructions for systemd.
Oh, and is the [ error ] ofAppGLFWWindow: 65544: X11: The DISPLAY environment variable is missing a problem? In that case: that reminds me of starting GUI applications from SSH, to run on the remote machine.
Maybe you need to call

export DISPLAY=:0 
xhost+

That’s at least what I do when I want to start OF-apps on my Ubuntu setup.

thanks for the pointers.
@Jildert I am a bit new to this. so I do not know where to put the export DISPLAY=:0 ? in to the .service ?
thanks a bunch

Now I think of it, I haven’t used that for my SC startup :sweat_smile:
Anyway, I called a .sh script from systemd, although I think putting everything in one line will do the job as well.
ExecStart=sudo export DISPLAY=:1; xhost+; sudo /home/pi/openFrameworks/apps/remotePulse/remotePulse/bin/remoteP$
Not sure if this will work.

I had some problems with the unavailable X session (same kind of errors with the DISPLAY variable), which I didn’t need anyway, but SC needed it to start.
So I used a fake X server (Xvfb) to connect to (since I didn’t use a monitor)
But for your case:
I assume the Raspberry boots into autologin desktop mode? So then the X server should be available…
I think the service may be called too soon? This may work (setting the service to idle)?
Can’t test right now, so I apologize if my suggestions may lead to confusion.

Edit: (now I see you’re errors are already in sudo service myProg status…)
Edit:
Starting services from user session (StackOverflow)

Thanks again.
I will try this now.

But I will also add some more findings here for my future self.


I already have this watchdog.sh that I use on my macOS machines. It checks a log file and if this log file was not updated in the last n seconds it tries to start my OF app.

I modified it a bit and it now also runs a PI. But only if I call it through the terminal direclty. All of these 3 ways to call it work.

/home/pi/Desktop/watchdog.sh
bash  /home/pi/Desktop/watchdog.sh
sudo bash  /home/pi/Desktop/watchdog.sh

So I thought I can could trigger this watchdog.sh via crontab. BUT the script does not run when using crontab.
I tried all of the following.

* * * * * bash /home/pi/Desktop/watchdog.sh
* * * * * sudo bash /home/pi/Desktop/watchdog.sh
* * * * * bash -x /home/pi/Desktop/watchdog.sh > /home/pi/Desktop/clog.log
* * * * * sudo bash -x /home/pi/Desktop/watchdog.sh > /home/pi/Desktop/clog.log

the log file does echo prints the .sh script but never opens the OF this way.

no success yet.

this is what my .service file looks like. which lives in /etc/systemd/system/hemmer.service

[Unit]
Description=hemmer
After=network.target

[Service]
ExecStart=sudo export DISPLAY=:1; xhost+; sudo /home/pi/openFrameworks/apps/remotePulse/remotePulse/bin/remotePulse
WorkingDirectory=/home/pi/openFrameworks/apps/remotePulse/remotePulse/bin
StandardOutput=inherit
StandardError=inherit
StartLimitInterval=5min
Restart=always
User=root

[Install]
WantedBy=multi-user.target

when calling:

pi@raspberrypi:/etc/systemd/system $ sudo systemctl status hemmer.service

this gets printed:

● hemmer.service - hemmer
   Loaded: loaded (/etc/systemd/system/hemmer.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Thu 2020-01-23 10:01:54 EST; 936ms ago
  Process: 1205 ExecStart=/usr/bin/sudo export DISPLAY=:1; xhost+; sudo /home/pi/openFrameworks/apps/remotePulse/remo
 Main PID: 1205 (code=exited, status=1/FAILURE)

Jan 23 10:01:54 raspberrypi systemd[1]: hemmer.service: Service RestartSec=100ms expired, scheduling restart.
Jan 23 10:01:54 raspberrypi systemd[1]: hemmer.service: Scheduled restart job, restart counter is at 5.
Jan 23 10:01:54 raspberrypi systemd[1]: Stopped hemmer.
Jan 23 10:01:54 raspberrypi systemd[1]: hemmer.service: Start request repeated too quickly.
Jan 23 10:01:54 raspberrypi systemd[1]: hemmer.service: Failed with result 'exit-code'.
Jan 23 10:01:54 raspberrypi systemd[1]: Failed to start hemmer.

I tried DISPLAY=:1 and DISPLAY=:0

have not yet found the magic mix to make it work with systemd.

But using crontab which calls my custom .sh script works now.

sudo crontab -e
then past in this line:
* * * * * bash /home/pi/Desktop/startMyApp.sh

a very simple startMyApp.sh script to start my OF app. Be careful crontab will open this app every minute. So, this is just for quick testing.

#!/bin/bash
export XAUTHORITY=/home/pi/.Xauthority
export DISPLAY=:0.0
echo "testing my script"

sleep 1
sudo /home/pi/openFrameworks/apps/remotePulse/remotePulse/bin/remotePulse &
echo "should have started app"