Continuously running a python script with node-RED

I have a folder located in /home/pi/myscript which contains a python script and 3 other files which the python file uses, such as a config and a private_key.pem.
To run the script via command prompt, I cd into /home/pi/myscript and then python3 bot.py and it works great.

However, I need this to run 24/7, and am looking for node-RED to assist with that.

My first thoughts were to use an exec node set to 'spawn mode' and use the command python3 /home/pi/oracle_script/bot.py to run the script.
It starts, but quickly errors, because the script cannot now find the other files in the /home/pi/myscript folder...

example error msg - oci.exceptions.ConfigFileNotFound: Could not find config file at ./config

Without editing & hard coding the paths in the bot.py script (I'm not the author), is there a way to overcome this problem, or a better way to run the python script 24/7?

Use systemd to execute the program during the startup process and to automatically restart it if it fails.

Communicate with Node-red through MQTT.

It's a poor second best but write a wrapper script to set up the environment and then call the python script.

imho "python3 bot.py" is a really poor way to execute a python script. Instead make the script executable, include a hashbang line to define the interpreter and invoke it by it's full pathname. Which should be /home/pi/bin/bot.py or /usr/local/bin/bot.py rather than /home/pi/myscript/bot.py

or maybe combine both commands into a single line

 cd /home/pi/myscript && python3 bot.py
1 Like

in spawn mode that would be python3 -u ?

With python. It’s always good to add -u :slight_smile:

Thanks Dave & jbudd

cd /home/pi/myscript && python3 -u bot.py appears to work OK, I can see the pid number, and have checked that it's running using top.

When I use a terminal to run the script, it prints results & errors in the terminal, but because the script continues to run in a loop, I never get any output from the exec node.
It's not essential, but it would be nice to see that output in node-RED, but I don't suppose it's possible.

You probably want to redirect stderr to stdout, so that would be
cd /home/pi/myscript && python3 -u ./bot.py 2>&1

looks at it and shudders

I'm afraid it's made no difference, still no output from the exec node.

You get nothing from either the first (stdout) or second (stderr) exec node output?
You are using spawn mode?

I tried a simple script

import time
i = 1
while i < 4:
   print (i)
   i += 1
   time.sleep(5)
print('Error', file=sys.stderr) # This is an error

As expected it sends 1, 2, 3 to stdout at 5 second intervals and an error message to stderr

No, if I select spawn mode, the exec node status shows rc:-2 and does not run, but it does run in exec mode...
For some reason, it doesn't spawn.

How do you know it runs if you don't see any output?

Try this increasingly unattractive invocation to capture any output
cd /home/pi/myscript && python3 -u ./bot.py >/home/pi/bot.pi.out 2>&1

By checking top it shows that the process is running.

I'm mystified then, sorry!

No problem, I've tried your latest, and it still fails.
But as I said above;

So thanks for the help, but lets leave it here :smiley:

Not wanting to make a bad situation worse. (As I usually do)

As suggested if you get the script to talk to MQTT you could send a message to MQTT every loop of the code.

Then listen to the MQTT-IN message, put a trigger node and if it doesn't get a message to stop it timing out, it sends a message saying the script has stopped.

Just a thought.
Little extra overhead.

Thanks Andrew, but as I said above;

The script now runs continuously, which was the main reason for this topic. I can manage OK without the logs.

Sorry Paul. I missed that bit. Nedd glasses. :wink:
(As you ca nsee)
:rofl: Sorry.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.