# Python Script that should feed node-red via MQTT does not start as a service

**URL:** <https://discourse.nodered.org/t/python-script-that-should-feed-node-red-via-mqtt-does-not-start-as-a-service/69426>\
**Category:** General\
**Created:** [22 October 2022 15:53 UTC](https://discourse.nodered.org/t/python-script-that-should-feed-node-red-via-mqtt-does-not-start-as-a-service/69426 "2022-10-22T15:53:22Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [22 October 2022 15:53 UTC](https://discourse.nodered.org/t/python-script-that-should-feed-node-red-via-mqtt-does-not-start-as-a-service/69426/1 "2022-10-22T15:53:22Z")

</div>

Hi all,

I have one raspberry pi running with node-red installed on - everthing is fine.  
In parallel there is a mosquitto mqtt broker running - minor issues, but basically fine

Next I would like to automatically run a Python Script that reads data from Powermeter via serial bus, converting the data into MQTT packages, forwarding them to NodeRed.

Whenever I start the script manually, it works fine  
When I start the script as a service - following this how-to,

> _Now that we have our service we need to activate it:_
> 
> _sudo chmod 644 /lib/systemd/system/hello.service_  
> _chmod +x /home/pi/hello\_world.py_  
> _sudo systemctl daemon-reload_  
> _sudo systemctl enable hello.service_  
> _sudo systemctl start hello.service_

it does not start and a status query gives the following error message

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/c/4c0d8cd7548afd123af592730b741d850b98ff10.png)

If I manually start the service with

> sudo systemctl start stromzaehler.service

it again works fine.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/5/d59ae3ba402a994f24bf06f8069e19b897fd375e.png)

Does it make sense to wait for a certain time after a raspi restart to run this script ?  
Or is there another error ?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 October 2022 16:25 UTC](https://discourse.nodered.org/t/python-script-that-should-feed-node-red-via-mqtt-does-not-start-as-a-service/69426/2 "2022-10-22T16:25:18Z")

</div>

It isn't very clear exactly what problem you are having. Do you mean that the service starts ok when started manually with systemctl, but not when starting automatically on boot?

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [22 October 2022 16:37 UTC](https://discourse.nodered.org/t/python-script-that-should-feed-node-red-via-mqtt-does-not-start-as-a-service/69426/3 "2022-10-22T16:37:13Z")

</div>

Yes, it does not start during/shortly after bootup (what I expect it to do with these commands):  
_Now that we have our service we need to activate it:_

_sudo chmod 644 /lib/systemd/system/stromzaehler.service_  
_chmod +x /home/pi/stromzaehler.py_  
_sudo systemctl daemon-reload_  
_sudo systemctl enable stromzaehler.service_  
_sudo systemctl start stromzaehler.service_

But when I start it manually as soon as the raspi is up and running, it starts fine without any issues.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 October 2022 16:40 UTC](https://discourse.nodered.org/t/python-script-that-should-feed-node-red-via-mqtt-does-not-start-as-a-service/69426/4 "2022-10-22T16:40:39Z")

</div>

Is the python posting to the local mosquitto on that machine?

If you stop mosquitto and then manually start your script does it fail?

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [22 October 2022 16:42 UTC](https://discourse.nodered.org/t/python-script-that-should-feed-node-red-via-mqtt-does-not-start-as-a-service/69426/5 "2022-10-22T16:42:24Z")

</div>

Yes it does

The script exits with a "broker not found" error when I turn off mosquitto

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [22 October 2022 16:58 UTC](https://discourse.nodered.org/t/python-script-that-should-feed-node-red-via-mqtt-does-not-start-as-a-service/69426/6 "2022-10-22T16:58:22Z")

</div>

> [@Gawan](#):
>
> stromzaehler.service

what is the exact code of your service file?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 October 2022 17:09 UTC](https://discourse.nodered.org/t/python-script-that-should-feed-node-red-via-mqtt-does-not-start-as-a-service/69426/7 "2022-10-22T17:09:58Z")

</div>

Are you waiting for the network to be up in the service script?

---

<div class="post-metadata">

**Author:** ![wb666greene](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wb666greene/32/6534_2.png) [@wb666greene](https://discourse.nodered.org/u/wb666greene)\
**Post date:** [22 October 2022 18:27 UTC](https://discourse.nodered.org/t/python-script-that-should-feed-node-red-via-mqtt-does-not-start-as-a-service/69426/8 "2022-10-22T18:27:40Z")

</div>

I use a quick'n'dirty approach to start the Python code that runs my security DVR AI system. I simply use an inject node set to "once on startup" to trigger ab exec node that launches my Python code with node-red running as a service on boot.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [22 October 2022 20:11 UTC](https://discourse.nodered.org/t/python-script-that-should-feed-node-red-via-mqtt-does-not-start-as-a-service/69426/9 "2022-10-22T20:11:46Z")

</div>

If the python code does not need to be running when node-red is not running, and it can be run as the node-red user, then indeed, starting it from an exec node is a perfectly good way to go.

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [22 October 2022 23:54 UTC](https://discourse.nodered.org/t/python-script-that-should-feed-node-red-via-mqtt-does-not-start-as-a-service/69426/10 "2022-10-22T23:54:04Z")

</div>

that sounds like a really good idea - this gives me full control on the script-schedule

is it difficult to start a Python Script from within Node-Red ?

---

<div class="post-metadata">

**Author:** ![wb666greene](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/wb666greene/32/6534_2.png) [@wb666greene](https://discourse.nodered.org/u/wb666greene)\
**Post date:** [23 October 2022 05:16 UTC](https://discourse.nodered.org/t/python-script-that-should-feed-node-red-via-mqtt-does-not-start-as-a-service/69426/11 "2022-10-23T05:16:40Z")

</div>

No it is quite easy, I usually make a shell script and put in it the commands I need to set things up,like changing to a certain directory, initializing the OpenVINO support, etc. Gives me tremendous flexibility.

Here is a sample script I use to start my AI code:

```auto
#!/bin/bash
# edit for directory of AI code and model directories,
# Making one symlink beats changing every script and/or multiple node-red nodes
# make symlink to A directory, for example:
# sudo ln -s /home/wally /home/AI
cd /home/AI/AI

export DISPLAY=:0
export XAUTHORITY=/home/AI/.Xauthority

# should be clean shutdown
/usr/bin/pkill -2 -f "AI.py" > /dev/null 2>&1
sleep 5

# but, make sure it goes away before retrying
/usr/bin/pkill -9 -f "AI.py" > /dev/null 2>&1
sleep 1

export PYTHONUNBUFFERED=1
# necessary only if using OpenVINO cv2
##source /opt/intel/openvino/bin/setupvars.sh
##python3 AI_dev.py -nNCS 1 -nTPU 1 -nt 1 -d 1 -cam onvif.txt >> ../detect/`/bin/date +%F`_AI.log 2>&1 &
python3 AI.py -d 0 -nTPU 1 -y4v -cam 6onvif.txt 2>/dev/null >> ../detect/`/bin/date +%F`_AI.log &

```

Here is the exec node for it:

```auto
[
    {
        "id": "be9701c3.1cb4b",
        "type": "inject",
        "z": "619024c0.35593c",
        "name": "Launch AI",
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "repeat": "",
        "crontab": "",
        "once": true,
        "onceDelay": "2",
        "x": 120,
        "y": 1146,
        "wires": [
            [
                "9497f40a.2d2718"
            ]
        ]
    },
    {
        "id": "9497f40a.2d2718",
        "type": "exec",
        "z": "619024c0.35593c",
        "command": "/home/AI/AI/StartAI.sh",
        "addpay": false,
        "append": "",
        "useSpawn": "false",
        "timer": "",
        "oldrc": false,
        "name": "Autostart",
        "x": 460,
        "y": 1146,
        "wires": [
            [
                "9ae30d45.83d05"
            ],
            [
                "9ae30d45.83d05"
            ],
            []
        ]
    }
]

```

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [23 October 2022 09:02 UTC](https://discourse.nodered.org/t/python-script-that-should-feed-node-red-via-mqtt-does-not-start-as-a-service/69426/12 "2022-10-23T09:02:07Z")

</div>

> [@Gawan](#):
>
> is it difficult to start a Python Script from within Node-Red

You may not need the sophistication of @wb666greene's script. You may well find that it is enough just to run the command in the exec node directly  
`/usr/bin/python /full/path/to/script.py`

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [24 October 2022 06:56 UTC](https://discourse.nodered.org/t/python-script-that-should-feed-node-red-via-mqtt-does-not-start-as-a-service/69426/13 "2022-10-24T06:56:14Z")

</div>

thats works exactly as expected ! perfect !

But I had to raise the wait time up to 20 seconds .... otherwise the Node-Red + Mosquitto service is not available on my Raspi 🙂

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [26 October 2022 20:33 UTC](https://discourse.nodered.org/t/python-script-that-should-feed-node-red-via-mqtt-does-not-start-as-a-service/69426/14 "2022-10-26T20:33:51Z")

</div>

This worked fine for me ... for some days.  
Then the script failed and it is in an error mode now.  
I would like to stop and restart the Python script from within Node-Red ?

Is this possible as well ?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [26 October 2022 20:45 UTC](https://discourse.nodered.org/t/python-script-that-should-feed-node-red-via-mqtt-does-not-start-as-a-service/69426/15 "2022-10-26T20:45:26Z")

</div>

You can send a kill command to the exec node. Read the node's help text. Then you can run it again.

---

<div class="post-metadata">

**Author:** ![Gawan](https://avatars.discourse-cdn.com/v4/letter/g/e9c0ed/32.png) [@Gawan](https://discourse.nodered.org/u/Gawan)\
**Post date:** [1 November 2022 12:32 UTC](https://discourse.nodered.org/t/python-script-that-should-feed-node-red-via-mqtt-does-not-start-as-a-service/69426/16 "2022-11-01T12:32:28Z")

</div>

Yes, this works perfectly fine now 😀 👍 👍

---

<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:** [15 November 2022 12:32 UTC](https://discourse.nodered.org/t/python-script-that-should-feed-node-red-via-mqtt-does-not-start-as-a-service/69426/17 "2022-11-15T12:32:31Z")

</div>

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