skip to Main Content

I’m trying to fix this issue:

 crawl_oled.service - test OLED SSD1306
     Loaded: loaded (/lib/systemd/system/crawl_oled.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Wed 2024-03-06 08:30:14 EST; 2min 33s ago
    Process: 200235 ExecStartPre=/bin/sleep 30 (code=exited, status=0/SUCCESS)
    Process: 200600 ExecStart=/usr/bin/python3 /home/pi/luma-env/luma.examples/examples/OLED_INFO.py (code=exited, status=1/FAILURE)
   Main PID: 200600 (code=exited, status=1/FAILURE)
        CPU: 609ms

Mar 06 08:30:14 raspberrypi python3[200600]: Traceback (most recent call last):
Mar 06 08:30:14 raspberrypi python3[200600]:   File "/home/pi/luma-env/luma.examples/examples/OLED_INFO.py", line 299, in <module>
Mar 06 08:30:14 raspberrypi python3[200600]:     run_ext()
Mar 06 08:30:14 raspberrypi python3[200600]:   File "/home/pi/luma-env/luma.examples/examples/OLED_INFO.py", line 59, in run_ext
Mar 06 08:30:14 raspberrypi python3[200600]:     random_py = random.choice(Local_List)
Mar 06 08:30:14 raspberrypi python3[200600]:   File "/usr/lib/python3.9/random.py", line 347, in choice
Mar 06 08:30:14 raspberrypi python3[200600]:     return seq[self._randbelow(len(seq))]
Mar 06 08:30:14 raspberrypi python3[200600]: IndexError: list index out of range
Mar 06 08:30:14 raspberrypi systemd[1]: crawl_oled.service: Main process exited, code=exited, status=1/FAILURE`
Mar 06 08:30:14 raspberrypi systemd[1]: crawl_oled.service: Failed with result 'exit-code'.

pi@raspberrypi:~ $ cat /etc/os-release  PRETTY_NAME="Debian GNU/Linux 11 (bullseye)" NAME="Debian GNU/Linux" VERSION_ID="11" VERSION="11 (bullseye)" VERSION_CODENAME=bullseye ID=debian HOME_URL="https://www.debian.org/" SUPPORT_URL="https://www.debian.org/support" BUG_REPORT_URL="https://bugs.debian.org/"

The script used to run normally at startup but have failed since I added the random function to randomise script launching from Luma Examples.

Here’s the part from the script that is failing

def run_ext():
    i = 0
    while True:
        try:
            # Command to run "OTHER".py
            ####random_file=random.choice(os.listdir())
            Local_List = [each for i, each in enumerate(os.listdir(current_working_directory)) if each.endswith('1.py')]
            random_py = random.choice(Local_List)
            random_py = current_working_directory + '/' + str(random_py)
            print(random_py)
            sp.run(random_py, timeout=10)
        except sp.TimeoutExpired:
            break 

When introducing i to random_py = random.choice(Local_List[i]), I get this following error:

crawl_oled.service - test OLED SSD1306
     Loaded: loaded (/lib/systemd/system/crawl_oled.service; enabled; vendor preset: enabled)
     Active: failed (Result: exit-code) since Wed 2024-03-06 08:52:57 EST; 2s ago
    Process: 216809 ExecStartPre=/bin/sleep 30 (code=exited, status=0/SUCCESS)
    Process: 217172 ExecStart=/usr/bin/python3 /home/pi/luma-env/luma.examples/examples/OLED_INFO.py (code=exited, status=1/FAILURE)
   Main PID: 217172 (code=exited, status=1/FAILURE)
        CPU: 545ms

Mar 06 08:52:56 raspberrypi systemd[1]: Started test OLED SSD1306.
Mar 06 08:52:57 raspberrypi python3[217172]: /
Mar 06 08:52:57 raspberrypi python3[217172]: Traceback (most recent call last):
Mar 06 08:52:57 raspberrypi python3[217172]:   File "/home/pi/luma-env/luma.examples/examples/OLED_INFO.py", line 299, in <module>
Mar 06 08:52:57 raspberrypi python3[217172]:     run_ext()
Mar 06 08:52:57 raspberrypi python3[217172]:   File "/home/pi/luma-env/luma.examples/examples/OLED_INFO.py", line 59, in run_ext
Mar 06 08:52:57 raspberrypi python3[217172]:     random_py = random.choice(Local_List[i])
Mar 06 08:52:57 raspberrypi python3[217172]: IndexError: list index out of range
Mar 06 08:52:57 raspberrypi systemd[1]: crawl_oled.service: Main process exited, code=exited, status=1/FAILURE
Mar 06 08:52:57 raspberrypi systemd[1]: crawl_oled.service: Failed with result 'exit-code'.

Can someone teach me how to fix this issue and what’s the best way to have this script work as expected:
pick a random example from the Luma Examples folder and run it for x seconds then go on with the rest of the main().
Worth noting that this same script run normally without error when lunched from Geany.

2

Answers


  1. Chosen as BEST ANSWER

    Same happens when using 'glob' library, the script start when lunched from Geany but give this message :

    crawl_oled.service - test OLED SSD1306 Loaded: loaded (/lib/systemd/system/crawl_oled.service; enabled; vendor preset: enabled) Active: failed (Result: exit-code) since Sat 2024-03-09 04:14:43 EST; 36s ago Process: 763 ExecStartPre=/bin/sleep 30 (code=exited, status=0/SUCCESS) Process: 1983 ExecStart=/usr/bin/python3 /home/pi/luma-env/luma.examples/examples/OLED_INFO.py (code=exited, status=1/FAILURE) Main PID: 1983 (code=exited, status=1/FAILURE) CPU: 1.027s

    Mar 09 04:14:43 raspberrypi python3[1983]: Within the precise time range. Execute the code. (4:14) Mar 09 04:14:43 raspberrypi python3[1983]: Traceback (most recent call last): Mar 09 04:14:43 raspberrypi python3[1983]: File "/home/pi/luma-env/luma.examples/examples/OLED_INFO.py", line 381, in Mar 09 04:14:43 raspberrypi python3[1983]: run_ext() Mar 09 04:14:43 raspberrypi python3[1983]: File "/home/pi/luma-env/luma.examples/examples/OLED_INFO.py", line 131, in run_ext Mar 09 04:14:43 raspberrypi python3[1983]: assert local_files Mar 09 04:14:43 raspberrypi python3[1983]: AssertionError Mar 09 04:14:43 raspberrypi systemd[1]: crawl_oled.service: Main process exited, code=exited, status=1/FAILURE Mar 09 04:14:43 raspberrypi systemd[1]: crawl_oled.service: Failed with result 'exit-code'. Mar 09 04:14:43 raspberrypi systemd[1]: crawl_oled.service: Consumed 1.027s CPU time.

    When lunched by Systemctl at startup.

    The answer reside in the fact that I'm using :

    current_working_directory = os.getcwd()
    

    to get the actual path but apparently systemctl provide his own path (or something similar) which get things wrong !

    It has been replaced by the exact path and now it's working as expected at boot-up. But still, I don't understand the fact that it cannot get the exact path of the script when lunched by Systemctl and still can use the Luma environment! That's confusing me a bit...


  2. The error message suggests that the list is empty. In other words, the "problem" is that there are no files ending with 1.py in current_working_directory. Look in a different directory, change the pattern, or add some files which actually match your pattern.

    For debugging this, probably simply print the list just before the line which raises the exception. This should allow you to see whether the value matches your expectations, and if not, investigate why not. This basic debugging technique is essential, and will take you very far once you master it.

    You haven’t shown us how you launch this script from systemd; if you haven’t specified a WorkingDirectory= directory in the [Service] section of the unit file, systemd will run it in /, the root directory, which is definitely not where you should store your Python files, even if you technically could, if you have root access.

    Tangentially, the code to populate the list is pretty obscure and complex. You might want to replace it with something like

    from pathlib import Path
    
    ...
            cwdobj = Path(current_working_directory)
            local_files = list(cwdobj.glob("*1.py"))
            # fail if there are no matches
            assert local_files
            random_py = random.choice(local_files)
            sp.run(str(random_py), timeout=10)
    

    The legacy glob library has roughly similar functionality, but I think you’ll appreciate how pathlib.Path encapsulates a directory entry in an object which you can query for its base name, file existence, file type, etc etc, as well as turn into a full path by simply converting it back to a string, like here.

    This won’t fix the problem you are struggling with, but simplifies your code to the point where it should be easier to debug it and/or develop it further.

    (Also, if current_working_directory is what it says it is, this variable is entirely redundant. The operating system knows at all times which directory you are in. Perhaps see also What exactly is current working directory?)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search