1

I can start a service called nodeserver.service and when I check its status it is active. But if I check its status again after a second or two it becomes failed.

The service itself looks like this,

[Unit]
Description=Node.js Example Server
#Requires=After=mysql.service       # Requires the mysql service to run first

[Service]
#ExecStart=/usr/bin/node /opt/nodeserver/app.js
ExecStart=/usr/bin/node /opt/nodeserver/expressTempApp/tempApp/app.js
Restart=always
RestartSec=20                       # Restart service after 10 seconds if node service crashes
StandardOutput=syslog               # Output to /var/log/syslog
StandardError=syslog                # Output to /var/log/syslog
SyslogIdentifier=nodejs-example
#User=pi
#Group=root
Environment=NODE_ENV=production PORT=8080

[Install]
WantedBy=multi-user.target

I can start the service but after about ten seconds it becomes inactive or failed

 pi at raspberrypi in /etc/systemd/system
$ sudo systemctl start nodeserver.service 
pi at raspberrypi in /etc/systemd/system
$ sudo systemctl status nodeserver.service 
● nodeserver.service - Node.js Example Server
   Loaded: loaded (/etc/systemd/system/nodeserver.service; enabled)
   Active: active (running) since Sat 2017-07-29 22:01:16 IST; 965ms ago
 Main PID: 15758 (node)
   CGroup: /system.slice/nodeserver.service
           └─15758 /usr/bin/node /opt/nodeserver/expressTempApp/tempApp/app.js

Jul 29 22:01:16 raspberrypi systemd[1]: Started Node.js Example Server.
pi at raspberrypi in /etc/systemd/system
$ sudo systemctl status nodeserver.service 
● nodeserver.service - Node.js Example Server
   Loaded: loaded (/etc/systemd/system/nodeserver.service; enabled)
   Active: failed (Result: start-limit) since Sat 2017-07-29 22:01:22 IST; 9s ago
  Process: 15803 ExecStart=/usr/bin/node /opt/nodeserver/expressTempApp/tempApp/app.js (code=exited, status=0/SUCCESS)
 Main PID: 15803 (code=exited, status=0/SUCCESS)

Jul 29 22:01:22 raspberrypi systemd[1]: nodeserver.service holdoff time over, scheduling restart.
Jul 29 22:01:22 raspberrypi systemd[1]: Stopping Node.js Example Server...
Jul 29 22:01:22 raspberrypi systemd[1]: Starting Node.js Example Server...
Jul 29 22:01:22 raspberrypi systemd[1]: nodeserver.service start request repeated too quickly, refusing to start.
Jul 29 22:01:22 raspberrypi systemd[1]: Failed to start Node.js Example Server.
Jul 29 22:01:22 raspberrypi systemd[1]: Unit nodeserver.service entered failed state.

the bottom few lines of the /var/log/syslog look like this,

Jul 29 22:01:21 raspberrypi systemd[1]: nodeserver.service holdoff time over, scheduling restart.
Jul 29 22:01:21 raspberrypi systemd[1]: Stopping Node.js Example Server...
Jul 29 22:01:21 raspberrypi systemd[1]: Starting Node.js Example Server...
Jul 29 22:01:21 raspberrypi systemd[1]: Started Node.js Example Server.
Jul 29 22:01:22 raspberrypi systemd[1]: nodeserver.service holdoff time over, scheduling restart.
Jul 29 22:01:22 raspberrypi systemd[1]: Stopping Node.js Example Server...
Jul 29 22:01:22 raspberrypi systemd[1]: Starting Node.js Example Server...
Jul 29 22:01:22 raspberrypi systemd[1]: nodeserver.service start request repeated too quickly, refusing to start.
Jul 29 22:01:22 raspberrypi systemd[1]: Failed to start Node.js Example Server.
Jul 29 22:01:22 raspberrypi systemd[1]: Unit nodeserver.service entered failed state.

Any help would be greatky appreciated, thanks

1

There are some things mixed up with your Unit file. First of all, don't use Restart= to try to "fix" a buggy service. It never helps and it's only waste of performance and confuses the system. You must use a stable service of course and have to fix bugs on it.

You want to run a service. Usually a service is staying in the background and always running. Then it can be terminated by sending a SIGNAL, which is managed by systemd with a systemctl stop <name-of.service>. The service MUST stop with a correct exit code, because it is processed by systemd.

It is also possible to run a service short one time, e.g. to initialize things and then stop regular without error, means with exit code 0. This can be managed with Type=oneshot. If you want to restart such a service regular after a time you can use the Restart=on-success option.

So first you have to know how your script app.js should work: running all the time in the background, or run a short time to do things one time and then stop regular without error. From your logging output the script is running short one time with status=0/SUCCESS:

 Process: 15803 ExecStart=/usr/bin/node /opt/nodeserver/expressTempApp/tempApp/app.js (code=exited, status=0/SUCCESS)
Main PID: 15803 (code=exited, status=0/SUCCESS)

but you are assuming that it should stay in the background always running. This discrepancy is the reason of your problems. I will give short examples for the three possibilities I have noted.

Always running:

[Unit]
Description=Node.js Example Server always running

[Service]
Environment=NODE_ENV=production PORT=8080
ExecStart=/usr/bin/node /opt/nodeserver/expressTempApp/tempApp/app.js

[Install]
WantedBy=multi-user.target

Short run one time:

[Unit]
Description=Node.js Example Server short run for init things

[Service]
Type=oneshot
RemainAfterExit=yes
Environment=NODE_ENV=production PORT=8080
ExecStart=/usr/bin/node /opt/nodeserver/expressTempApp/tempApp/app.js

[Install]
WantedBy=multi-user.target

Repeated short run after 20 sec:

[Unit]
Description=Node.js Example Server repeat running

[Service]
Type=oneshot
Restart=on-success
RestartSec=20
Environment=NODE_ENV=production PORT=8080
ExecStart=/usr/bin/node /opt/nodeserver/expressTempApp/tempApp/app.js

[Install]
WantedBy=multi-user.target
| improve this answer | |
0

The service was set to 'enable' which of course restarts it when and if it stops or on reboot of the pi. But this wasn't able to work with

RestartSec=20 

So by disabling the service with,

sudo systemctl disable nodeserver.service

Works now!

| improve this answer | |
  • Enabling a service does not enable automatic restart, unless the service unit file requests automatic restart behavior. You also did not set the service type, which should be simple, so it is clear that this service does not fork. – TheDiveO Jul 30 '17 at 16:15
  • How do I set a service type? – Shane G Jul 30 '17 at 16:31
  • BTW I posted the question. Some mix up when I tried to login and it used my email address and used my name and now username. – Shane G Jul 30 '17 at 16:32
  • Per freedesktop.org/software/systemd/man/systemd.service.html the default service type is "simple", so leaving it out seems to be fine. – TheDiveO Jul 30 '17 at 18:53
  • Please accept your own answer with a click on the tick on its left side. Only this will finish the question and it will not pop up again year for year. – Ingo Jan 30 at 13:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.