How to run a node js app on my NR box?

Guys,

I have just found a Node.js app that is run from the command line as follows

node tuya-mqtt.js

This app lets me control and monitor my new heatpump system using mqtt

I run node red on a VM so have plenty of grunt and memory available - is there an easy way to get this app to run in the background on startup of the machine ?

I know this is not strictly a NR question - but doing a google search has led me down a rabbit warren and i do not want to bugger up the machine running my main NR flow

Craig

What OS are you running on?
If it is Debian based such as Ubuntu or Raspbian then a simple systemd script should do it.

Sorry - should have said.

yep Ubuntu based in a VM - 18LTS

regards

Craig

OK just going through now and trying to set it up as a service with systemd as the user pi

Working through a few issues now

Craig

If it just runs as root the "old fashioned easy way" is just to add it to /etc/rc.local - you would need to fully path both the executable and the script name but is an easy fix.

yeah seems to run fine as root - just trying to get it to run as a restricted user (pi) and having problems as it does not seem to understand paths to find the config files - just trying to work through permutations on that before i thrown in the towel and just run it as root

Craig

In fact it must be something i am doing/not understanding.

I can run it from the commandline as the pi user from within its own directory (under the home directory for the pi user) - but when i run it as a service it is having problems - must be something to do with paths to pass into the service - just trying to understand how to do that one

Craig

When you run from systemd, don't forget that you don't get any of the usual environment variables - like path. However, there are ways to inherit them if you need to. You'll need to look that up I'm afraid, systemd has too many options to just remember :grinning:

Yeah thanks Julian - i think it is the same boat i am in at the moment - just trying to work through the permutations to make that happen !

Craig

OK for anyone else that wants to use this nodejs app (which produces much cleaner output than the other Tuya apps i could find)

The solution i ended up finding was on the openhab forums

To create it as a service

To run tuya-mqtt as a systemd-service, you can create a file:

 sudo vi /lib/systemd/system/tuya-mqtt.service

and add the following lines:

 [Unit]
 Description=tuya-mqtt
 After=mosquitto.service

 [Service]
 ExecStart=/usr/bin/node /home/pi/tuya-mqtt/tuya-mqtt.js
 Restart=always
 User=pi
 Group=pi
 Environment=PATH=/usr/bin/
 Environment=NODE_ENV=production
 WorkingDirectory=/home/pi/tuya-mqtt/

 [Install]
 WantedBy=multi-user.target

After saving the file, you have to run:

 sudo systemctl daemon-reload

Now you can start the service:

 sudo systemctl start tuya-mqtt.service

Check if the service is runnning properly:

sudo systemctl status tuya-mqtt.service

Post #1 in this link

Craig

1 Like

Do you really mean hom/pi on the execstart line ?

Good Spotting - sorry for the typo - fixed now !

Craig

Did you mean to include the space?

If you mean in between /usr/bin/node and then/home/pi etc etc

Then yes

It is saying to run the Node program and then run the javascript file in the tuya-mqtt directory (or are you saying there is another space somewhere ?

Craig

1 Like

Don't you need to do sudo systemctl enable tuya-mqtt.service also if you want the thing to start at reboot

Yep - Exactly - i think that was in the link that i provided also - but good to point out thanks

Craig

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