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

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

If I manually start the service with

sudo systemctl start stromzaehler.service

it again works fine.

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

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?

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

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

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

Yes it does

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

what is the exact code of your service file?

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

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.

1 Like

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.

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 ?

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:

# 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 "" > /dev/null 2>&1
sleep 5

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

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

Here is the exec node for it:

        "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": [
        "id": "9497f40a.2d2718",
        "type": "exec",
        "z": "619024c0.35593c",
        "command": "/home/AI/AI/",
        "addpay": false,
        "append": "",
        "useSpawn": "false",
        "timer": "",
        "oldrc": false,
        "name": "Autostart",
        "x": 460,
        "y": 1146,
        "wires": [
1 Like

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/

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 :slight_smile:

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 ?

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

Yes, this works perfectly fine now :grinning: :+1: :+1:

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