Wiki

Clone wiki

PyRTEdoc / Hello World!

If you don't really have time to go into details of Legato and develop a Legato app, alternatively you can follow the concept of this example which,

  • create a Python script file helloworld.py
  • run the helloworld.py when device boots up by creating start.sh script file

Create helloworld.py

First, ssh in the device and create helloworld.py in /home/root/myapp directory,

root@fx30s:~# pwd
/home/root
root@fx30s:~# mkdir myapp
root@fx30s:~# vi myapp/helloworld.py

Add these code in helloworld.py,

import logging
import logging.config
import logging.handlers

if __name__ == "__main__":
    root_logger = logging.getLogger()
    root_logger.setLevel(logging.DEBUG)

    formatter = logging.Formatter("%(name)-18s %(message)s")
    console_handler = logging.StreamHandler()
    console_handler.setLevel(logging.DEBUG)
    console_handler.setFormatter(formatter)
    root_logger.addHandler(console_handler)

    # add a rotating handler
    rotating_handler = logging.handlers.RotatingFileHandler("/tmp/myapp_log", maxBytes=2000, backupCount=2)
    root_logger.addHandler(rotating_handler)

    root_logger.info("Hello World!")

Give it a go,

root@fx30s:~# python3 myapp/helloworld.py
root               Hello World!
root@fx30s:~# cat /tmp/myapp_log
Hello World!
root@fx30s:~#

Great you have run a Python script file, and next we can see how to run it during boot-up.

Create start.sh

PyRTE starts automatically when the device boots up, it will run a shell script(/home/root/myapp/start.sh) if exists, in which you can run your Python script.

Again, ssh in the device if not yet,

root@fx30s:~# pwd
/home/root
root@fx30s:~# mkdir myapp
root@fx30s:~# vi myapp/start.sh

In the start.sh, put in,

#!/bin/sh
python3 /home/root/myapp/helloworld.py

Make start.sh executable,

root@fx30s:~# chmod +x myapp/start.sh
root@fx30s:~# ls -l myapp/start.sh
-rwxr-xr-x    1 root     root            49 Jul 20 06:11 myapp/start.sh
root@fx30s:~#

Reboot the device,

root@fx30s:~# reboot

Then ssh in the device again, and check if helloworld.py has run by output the log file,

root@fx30s:~# cat /tmp/myapp_log
Hello World!
root@fx30s:~#

Updated