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
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.
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.