dharvey24
Posts: 28
Joined: Tue Dec 01, 2015 11:55 am

NPM start on start-up? Help wanted

Mon Apr 11, 2016 6:25 am

I have a reveal.js server which can be kicked off by typing NPM start in the correct folder. Can anyone advise how I can issue the same command on start-up? I've tried various command lines in crontab but I suspect I'm barking up the wrong tree entirely. Any pointers would be greatly appreciated.

Heater
Posts: 19569
Joined: Tue Jul 17, 2012 3:02 pm

Re: NPM start on start-up? Help wanted

Mon Apr 11, 2016 6:57 am

dhharvey24,

Assuming you are using Jessie, which you should be, you can start a node server process using the new init system, systemd.

Example:

I have a server node server called "propanel". The code for that lives in /home/pi/propanel/server.js

I create a systemd "service" file containing the following:

Code: Select all

[Service]
WorkingDirectory=/home/pi/propanel
ExecStart=/usr/local/bin/node --expose-gc /home/pi/propanel/server.js
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=propanel
User=root
Group=root
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target
As you see that specifies the command to run to start the server, the working directory to run it from. the user and group to run it as, where to write errors and logs etc, etc. The "WantedBy" tells when it should be run.

In your case you would put your "npm start" command in the "ExecStart" line.

I can now enable and disable running my propanel service at start up with the systemd commands:

Code: Select all

 
$ sudo systemctl enable  propanel
$ sudo systemctl disable  propanel
I can simply start it for testing with:

Code: Select all

 
$ sudo systemctl start   propanel
Do google "systemd Debian" for more info on this.
Slava Ukrayini.

dharvey24
Posts: 28
Joined: Tue Dec 01, 2015 11:55 am

Re: NPM start on start-up? Help wanted

Mon Apr 11, 2016 3:42 pm

I've followed the instructions but its still not running. Enable seems to work. Start even goes through without a problem, but the server doesn't fire up.

I've saved the pitch.service file in the /etc/systemd/system folder.

The contents are:

Code: Select all

[Service]
WorkingDirectory=/home/pi/Revealer.js
ExecStart=/home/pi/Revealer.js/notell/npm test
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=notell
User=root
Group=root
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target


Normally I would go to the /home/pi/Revealer.js/notell folder and type NPM test and it runs.

All help is much appreciated.

Heater
Posts: 19569
Joined: Tue Jul 17, 2012 3:02 pm

Re: NPM start on start-up? Help wanted

Mon Apr 11, 2016 5:31 pm

So maybe your "WorkingDirectory" line should be:

Code: Select all

WorkingDirectory=/home/pi/Revealer.js/notell
Seeing as that is the directory you change to before running the thing.

and your "ExecStart" line should be:

Code: Select all

ExecStart=/whatever/path/to/npm test
After all, in the example you give you have set WorkingDirectory to ..../Reaveler.js which is a file not a directory!

Play with it till you get it right.
Slava Ukrayini.

Heater
Posts: 19569
Joined: Tue Jul 17, 2012 3:02 pm

Re: NPM start on start-up? Help wanted

Mon Apr 11, 2016 5:36 pm

Don't forget you have redirected log messages and errors to syslog so you should be able to see them in /var/log/messages
Slava Ukrayini.

dharvey24
Posts: 28
Joined: Tue Dec 01, 2015 11:55 am

Re: NPM start on start-up? Help wanted

Tue Apr 12, 2016 9:22 am

sorry yes i noticed the same mistake with the .js file and changed the path. Still not firing.

the code now :

Code: Select all

[Service]
WorkingDirectory=/home/pi/Revealer.js/notell
ExecStart=/home/pi/Revealer.js/notell/sudo npm start
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=notell
User=root
Group=root
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target


i've check the messages sys log and nothing shows up at all. so i suspect its not firing at all. Does the service file, mine is pitch.service need to be chmod'ed?

Heater
Posts: 19569
Joined: Tue Jul 17, 2012 3:02 pm

Re: NPM start on start-up? Help wanted

Tue Apr 12, 2016 9:44 am

Wait a minute, this is not right:

Code: Select all

ExecStart=/home/pi/Revealer.js/notell/sudo npm start
That is trying to run the command "/home/pi/Revealer.js/notell/sudo" which I guess does not exist.

So I tweaked my propanel.service file to use "npm start" like you want to. It now looks like this:

Code: Select all

[Service]
WorkingDirectory=/home/pi/propanel
ExecStart=/usr/local/bin/npm start
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=propanel
User=root
Group=root
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target
This works fine. No sure if we need the full path to npm in the ExecStart line. Your npm may be on a different path. You can check with the command:

Code: Select all

$ which npm
/usr/local/bin/npm
$
The permissions on my propanel.serveice file are:

Code: Select all

-rwxr-xr-- 1 root root 256 Apr 12 12:32 /etc/systemd/system/propanel.service
Slava Ukrayini.

dharvey24
Posts: 28
Joined: Tue Dec 01, 2015 11:55 am

Re: NPM start on start-up? Help wanted (SOLVED)

Tue Apr 12, 2016 11:05 am

It works!!!! thank you so much. Saved me literally hours of searching.

for reference this is what my 'service' file looks like now. I also changed the permissions on the file to match yours.

Code: Select all

[Service]
WorkingDirectory=/home/pi/Revealer.js/notell
ExecStart=/usr/bin/npm start
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=notell
User=root
Group=root
Environment=NODE_ENV=production

[Install]
WantedBy=multi-user.target



Heater
Posts: 19569
Joined: Tue Jul 17, 2012 3:02 pm

Re: NPM start on start-up? Help wanted

Tue Apr 12, 2016 1:00 pm

Good show.

Sometimes one just has to stop searching and take a long look at one has written and try to understand what it is about.

Or just ask someone who has done all that already :)
Slava Ukrayini.

User avatar
OutsourcedGuru
Posts: 73
Joined: Mon Jun 19, 2017 11:15 pm

Re: NPM start on start-up? Help wanted

Fri Jan 25, 2019 10:16 pm

For anyone trying to follow along with Raspbian Stretch, I found that "/etc/systemd" was not the place where it wanted to see these "*.service" files. Also, the default file restrictions appear to be wrong as seen above.

Path:

Code: Select all

/lib/systemd/system/yourapp.service
Rights:

Code: Select all

sudo chmod 644 /lib/systemd/system/yourapp.service
Enable:

Code: Select all

sudo systemctl enable yourapp

Code: Select all

Created symlink /etc/systemd/system/multi-user.target.wants/yourapp.service → /lib/systemd/system/yourapp.service.
Start:

Code: Select all

sudo service yourapp start
Additionally, it appears to want a `[Unit]` paragraph at the top of that file as well:

Code: Select all

[Unit]
Description=My great app
After=network.start

Heater
Posts: 19569
Joined: Tue Jul 17, 2012 3:02 pm

Re: NPM start on start-up? Help wanted

Sun Jan 27, 2019 9:49 am

OutsourcedGuru,
For anyone trying to follow along with Raspbian Stretch, I found that "/etc/systemd" was not the place where it wanted to see these "*.service" files.
Correct, that is the wrong location for service files. Should be /etc/systemd/system.

I prefer not to mess with anything in /lib/systemd/. Better to add your customizations where they belong /etc/systemd/system.

I have never had to mess with changing file modes or creating symlinks when doing this. Just drop your service file into /etc/systemd/system and away you go.
Slava Ukrayini.

User avatar
OutsourcedGuru
Posts: 73
Joined: Mon Jun 19, 2017 11:15 pm

Re: NPM start on start-up? Help wanted

Mon Jan 28, 2019 4:49 pm

> "Just drop your service file into /etc/systemd/system and away you go."

I spent hours trying to get this "just" version to work. I'm suggesting that with Raspbian Stretch it needs to be in the way I'm describing.

I will note that the "After" line in the [Unit] section needs to instead be:

Code: Select all

After=network-online.target
Otherwise, it would throw an error during bootup: "Failed to add dependency on network.start, ignoring: Invalid argument".

Heater
Posts: 19569
Joined: Tue Jul 17, 2012 3:02 pm

Re: NPM start on start-up? Help wanted

Tue Jan 29, 2019 8:33 am

I'm suggesting it does not need to be done that way. I have never done it that way. It has always worked as I describe:

Here is an example service file I have on a Pi 3:

Code: Select all

[Service]
Type=simple
Restart=always
RestartSec=1
User=root
ExecStart=/home/pi/.nvm/versions/node/v9.3.0/bin/node /home/pi/sense-hat-test/index.js

[Install]
WantedBy=multi-user.target
Here is how it works:

Code: Select all

$ uname -a
Linux pi64 4.11.12-pi64+ #1 SMP PREEMPT Sun Jul 30 20:18:20 CEST 2017 aarch64 GNU/Linux
pi@pi64:~$ cat /etc/systemd/system/sense-hat-test.service
[Service]
Type=simple
Restart=always
RestartSec=1
User=root
ExecStart=/home/pi/.nvm/versions/node/v9.3.0/bin/node /home/pi/sense-hat-test/index.js

[Install]
WantedBy=multi-user.target

pi@pi64:~$ sudo systemctl start sense-hat-test
pi@pi64:~$ sudo systemctl enable sense-hat-test
Created symlink /etc/systemd/system/multi-user.target.wants/sense-hat-test.service → /etc/systemd/system/sense-hat-test.service.
pi@pi64:~$ sudo shutdown -r now
...
...
pi@pi64:~$  
After the reboot it comes up just fine.

Admittedly it's better to have the [unit] section in the service file:

Code: Select all

[Unit]
Description=Sense Hat test 
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
User=root
ExecStart=/home/pi/.nvm/versions/node/v9.3.0/bin/node /home/pi/sense-hat-test/index.js

[Install]
WantedBy=multi-user.target
It just works. No messing with file modes, symlinks or /lib/
Slava Ukrayini.

Return to “Other programming languages”