NPM start on start-up? Help wanted
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.
Re: NPM start on start-up? Help wanted
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:
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:
I can simply start it for testing with:
Do google "systemd Debian" for more info on this.
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
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
Code: Select all
$ sudo systemctl start propanel
Slava Ukrayini.
Re: NPM start on start-up? Help wanted
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:
Normally I would go to the /home/pi/Revealer.js/notell folder and type NPM test and it runs.
All help is much appreciated.
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
All help is much appreciated.
Re: NPM start on start-up? Help wanted
So maybe your "WorkingDirectory" line should be:
Seeing as that is the directory you change to before running the thing.
and your "ExecStart" line should be:
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.
Code: Select all
WorkingDirectory=/home/pi/Revealer.js/notell
and your "ExecStart" line should be:
Code: Select all
ExecStart=/whatever/path/to/npm test
Play with it till you get it right.
Slava Ukrayini.
Re: NPM start on start-up? Help wanted
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.
Re: NPM start on start-up? Help wanted
sorry yes i noticed the same mistake with the .js file and changed the path. Still not firing.
the code now :
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?
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
Re: NPM start on start-up? Help wanted
Wait a minute, this is not right:
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:
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:
The permissions on my propanel.serveice file are:
Code: Select all
ExecStart=/home/pi/Revealer.js/notell/sudo npm start
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
Code: Select all
$ which npm
/usr/local/bin/npm
$
Code: Select all
-rwxr-xr-- 1 root root 256 Apr 12 12:32 /etc/systemd/system/propanel.service
Slava Ukrayini.
Re: NPM start on start-up? Help wanted (SOLVED)
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.
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
Re: NPM start on start-up? Help wanted
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
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.
- OutsourcedGuru
- Posts: 73
- Joined: Mon Jun 19, 2017 11:15 pm
Re: NPM start on start-up? Help wanted
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:
Rights:
Enable:
Start:
Additionally, it appears to want a `[Unit]` paragraph at the top of that file as well:
Path:
Code: Select all
/lib/systemd/system/yourapp.service
Code: Select all
sudo chmod 644 /lib/systemd/system/yourapp.service
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.
Code: Select all
sudo service yourapp start
Code: Select all
[Unit]
Description=My great app
After=network.start
Re: NPM start on start-up? Help wanted
OutsourcedGuru,
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.
Correct, that is the wrong location for service files. Should be /etc/systemd/system.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.
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.
- OutsourcedGuru
- Posts: 73
- Joined: Mon Jun 19, 2017 11:15 pm
Re: NPM start on start-up? Help wanted
> "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:
Otherwise, it would throw an error during bootup: "Failed to add dependency on network.start, ignoring: Invalid argument".
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
Re: NPM start on start-up? Help wanted
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:
Here is how it works:
After the reboot it comes up just fine.
Admittedly it's better to have the [unit] section in the service file:
It just works. No messing with file modes, symlinks or /lib/
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
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:~$
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
Slava Ukrayini.