Auto Start Apps - How they work?

I have written / modified several apps now that I was able to get running on the FX30 with but always as start: manual. They run through once to do a function (get location, print Hello World) and then stop.

I have started looking at the code for a few apps that are auto start and continue running.

When I look at the code, I was expecting to see a WHILE Loop or something that would continue running in the background watching for changes / triggers, etc. but I don’t see that.

For example the sample MQTT app, legato-af/apps/sample/mqtt at master · legatoproject/legato-af · GitHub
is on auto start app for both publish and subscribe.

Normally, when subscribing to an MQTT topic, you don’t know when the updated topic may be sent, so the Subscribe service is running in the background and receiving topics as they come in and then reporting them, by nature this would need to be looping forever watching for the change.

I am not trying to modify the MQTT app, just using it as an example…

I have looked through about 10 different start: auto apps and none of them have WHILE loops like I was expecting…

So the question is: On Apps that are created to start automatically and staying running, does Legato just continue to run through the code in a loop top to bottom or is that supposed to be something done in the code? If has to be added in the code, then why do none of the sample apps that are auto start have while loops? How is it being accomplished?

Based on the Auto Run app I just created, the answer is NO, it doesn’t continue looping through the code.
I created an app that prints a counter value to a file, then sleeps for 5 seconds and it runs exactly one time and never increments the counter. So I guess that means Legato does not just keep iterating through the code if you put start: auto.

“start: auto” means it will be running after boot up, no need to do “app start xxx”

That makes sense.
Is there an example of an app that just uses auto:start and just loops forever?

I am not sure if that is just a while statement, or what the best way to accomplish that?

how about this?

Well, that was easy enough. I forgot about that one, and I did that demo just a few weeks ago.

Is there any downside to doing a never ending loop?

I am looking for a way to kick off several python scripts based on a timer and then I need something to monitor the python script to see if it has stopped running.

On the python side, I was thinking of writing a script to increment an integer once per minute to a tmpfs file in memory in python. 1-1000000 once per minute, for example

Then on the app side, run a continuous loop to monitor for integer change, sleep(61), compare values, if still the same, sleep(30), compare again, then force a reboot or kick off the python script again if possible (I haven’t figured out how to re-start a python script from an app however).

The python app I am using will automatically run a designated script in the myapp directory upon startup. So I should be able to put my main python asyncio program in there that will loop and fire off my python functions using a python scheduler or sleep timer.

Yes, I realize I am mimicking the watchdog, but I didn’t know if there was a way to use watchdog to call an external (python) program. And I also don’t know how the watchdog would know that the python script locked up or died without some sort of heartbeat. Which is why I was thinking of a endless loop…

Any thoughts with this approach?
Is there a way to re-start a python script using an app once I determine it stopped working?

You can make some stress test or overnight test to verify

Have you tried to use system() API in unsandboxed application?

So your saying I could use system API to re-initialize the python script if it dies?

Test for heartbeat:

If (Current == Last) {

#call python script to re-start

I would assume I could put the script in my home directory as well by using the xattr set ‘security.SMACK64 or just set sandboxed: false ? Otherwise I would have to generate the /tmp/ script during startup since it is not persistent on a reboot…

I thought of another option using the watchdog example found here: legato-af/apps/sample/watchdog/helloDaemon/daemon.c at master · legatoproject/legato-af · GitHub

 * Sample showing how to monitor a critical process with a watchdog.
 * In this case logging "Hello World" every 10s or so
 * Copyright (C) Sierra Wireless Inc.

#include "legato.h"
#include "interfaces.h"
#include "watchdogChain.h"

    // Try to kick a couple of times before each timeout.
    le_clk_Time_t watchdogInterval = { .sec = 8 };
    LE_INFO("Hello World!");
    le_wdogChain_MonitorEventLoop(0, watchdogInterval);

This sample is looking in the logs to see Hello World every few seconds.

I could create a small app (PyLoopChk) that just prints LE_INFO(“Python Loop Active”); and set it to Manual Start.

In my python code, I could call ‘app start PyLoopChk’ once per minute, and as long as the script is running, the app will be called, which will write ‘Python Loop Active’ to the LE_INFO log.

Then I could setup a watchdog just like the one posted above but modified to LE_INFO(“Python Loop Active”) which the watchdog would monitor.

If the python loop fails, the log would not be printed, so the system would be rebooted.

Would that work? This method doesn’t require a heartbeat and an endless loop app running 24x7.

Unsandboxed app can access all files

Yes, I understand. That is why I said I could put it in home and sandboxed: false

What about my other idea using Watchdog, do you think that would be better?

You cannot repeatedly call “app start”

Why? If it terminates in 2 seconds and I call it once per minute, that still would be a problem?

have you tried that?

Here shows an example to use faultaction and exit(1) to reboot the system:

You can start a sandboxed application with an one-shot timer to call exit(1).
If the application is not called by “app restart xxx”, it will just reboot the system.

Ok. Looking at that, I understand the fault action: reboot would be called if Hello App terminates?

But how would that apply to a python file?

If the application is not called by “app restart xxx”, it will just reboot the system.

I don’t understand what this means?
Python is supposed to call ‘app restart xxx’ ? Wouldn’t calling that once a minute be a problem as well?

you need to make some test to verify

actually you don’t even need legato application to implement this, just run a script which will reboot system after x seconds, and then your python script needs to kill this script and restart the script periodically

When I tried with my gpsAcquire script running every 10 seconds for about 10X.
app start gpsAcquire - every 10 seconds

It did freakout after about the 5 or 6th time and gave me bad coordinates and I had to get gnss fix 30 to get my ttff to start over.

I did a 2nd test with app restart gpsAcquire and ran it every 20 seconds 11X and it retrieved gps coordinates every time.

actually you don’t even need legato application to implement this, just run a script which will reboot system after x seconds, and then your python script needs to kill this script and restart the script periodically

Interesting idea. Basically if you don’t hear from me, reboot.

My concern was if somehow python gets corrupted, memory error, or script error. Then I can fall back on the default legato framework which has been way more tested than something that I am writing today.

I am a PLC programmer by trade, so we use heartbeats like I mentioned before on a regular basis when talking across a network to insure connectivity. I think I am going to try the heartbeat idea first with an endless loop app in legato, then use ‘top’ to determine it’s impact.

If it is not too resource heavy, then I can monitor for no change and signal reboot.

I still think it is crazy they don’t enable cron by default or some cron like functionality considering this device runs linux.

Thanks for the help!

I will let you know how it goes.

if you still want to use legato watchdog service, you can ask the python script to update one file in /tmp with date and time, then the legato application can start the watchdog and check the file content every minute, if it is not updated, then it can reboot by not kicking the watchdog by le_wdog_Kick().

BTW, here is another example showing sandboxed application is needed for watchdog service: