# Continuously running a python script with node-RED

**URL:** <https://discourse.nodered.org/t/continuously-running-a-python-script-with-node-red/76396>\
**Category:** General\
**Created:** [10 March 2023 16:32 UTC](https://discourse.nodered.org/t/continuously-running-a-python-script-with-node-red/76396 "2023-03-10T16:32:13Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [10 March 2023 16:32 UTC](https://discourse.nodered.org/t/continuously-running-a-python-script-with-node-red/76396/1 "2023-03-10T16:32:13Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [10 March 2023 16:54 UTC](https://discourse.nodered.org/t/continuously-running-a-python-script-with-node-red/76396/2 "2023-03-10T16:54:30Z")

</div>

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

Communicate with Node-red through MQTT.

> [@Paul-Reed](#):
>
> Without editing & hard coding the paths in the `bot.py` script (I'm not the author), is there a way to overcome this problem?

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

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [10 March 2023 18:06 UTC](https://discourse.nodered.org/t/continuously-running-a-python-script-with-node-red/76396/3 "2023-03-10T18:06:36Z")

</div>

or maybe combine both commands into a single line

```auto
 cd /home/pi/myscript && python3 bot.py

```

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [10 March 2023 18:24 UTC](https://discourse.nodered.org/t/continuously-running-a-python-script-with-node-red/76396/4 "2023-03-10T18:24:59Z")

</div>

> [@dceejay](#):
>
> `cd /home/pi/myscript && python3 bot.py`

in spawn mode that would be python3 -u ?

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [10 March 2023 18:28 UTC](https://discourse.nodered.org/t/continuously-running-a-python-script-with-node-red/76396/5 "2023-03-10T18:28:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [10 March 2023 19:55 UTC](https://discourse.nodered.org/t/continuously-running-a-python-script-with-node-red/76396/6 "2023-03-10T19:55:24Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [10 March 2023 20:03 UTC](https://discourse.nodered.org/t/continuously-running-a-python-script-with-node-red/76396/7 "2023-03-10T20:03:17Z")

</div>

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_

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [10 March 2023 20:20 UTC](https://discourse.nodered.org/t/continuously-running-a-python-script-with-node-red/76396/8 "2023-03-10T20:20:33Z")

</div>

> [@jbudd](#):
>
> cd /home/pi/myscript && python3 -u ./bot.py 2\>&1

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

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [10 March 2023 21:57 UTC](https://discourse.nodered.org/t/continuously-running-a-python-script-with-node-red/76396/9 "2023-03-10T21:57:52Z")

</div>

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

I tried a simple script

```auto
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

 ![Untitled 1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/9/197f6a50ec146c11c70d502cc748e7c716cc411b.jpeg)

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [10 March 2023 22:17 UTC](https://discourse.nodered.org/t/continuously-running-a-python-script-with-node-red/76396/10 "2023-03-10T22:17:25Z")

</div>

> [@jbudd](#):
>
> You are using spawn mode?

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.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [10 March 2023 22:35 UTC](https://discourse.nodered.org/t/continuously-running-a-python-script-with-node-red/76396/11 "2023-03-10T22:35:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [10 March 2023 22:38 UTC](https://discourse.nodered.org/t/continuously-running-a-python-script-with-node-red/76396/12 "2023-03-10T22:38:02Z")

</div>

> [@jbudd](#):
>
> How do you know it runs if you don't see any output?

By checking `top` it shows that the process is running.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [10 March 2023 22:39 UTC](https://discourse.nodered.org/t/continuously-running-a-python-script-with-node-red/76396/13 "2023-03-10T22:39:04Z")

</div>

I'm mystified then, sorry!

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [10 March 2023 22:44 UTC](https://discourse.nodered.org/t/continuously-running-a-python-script-with-node-red/76396/14 "2023-03-10T22:44:20Z")

</div>

> [@jbudd](#):
>
> cd /home/pi/myscript && python3 -u ./bot.py \>/home/pi/bot.pi.out 2\>&1

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

> [@Paul-Reed](#):
>
> It's not essential, but it would be nice

So thanks for the help, but lets leave it here 😃

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [10 March 2023 22:50 UTC](https://discourse.nodered.org/t/continuously-running-a-python-script-with-node-red/76396/15 "2023-03-10T22:50:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [10 March 2023 22:59 UTC](https://discourse.nodered.org/t/continuously-running-a-python-script-with-node-red/76396/16 "2023-03-10T22:59:17Z")

</div>

Thanks Andrew, but as I said above;

> [@Paul-Reed](#):
>
> So thanks for the help, but lets leave it here 😃

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

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [10 March 2023 23:04 UTC](https://discourse.nodered.org/t/continuously-running-a-python-script-with-node-red/76396/17 "2023-03-10T23:04:40Z")

</div>

Sorry Paul. I missed that bit. Nedd glasses. 😉  
(As you ca nsee)  
🤣 Sorry.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [24 March 2023 23:05 UTC](https://discourse.nodered.org/t/continuously-running-a-python-script-with-node-red/76396/18 "2023-03-24T23:05:38Z")

</div>

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