8

I need to run my node.js script automatically from my remote machine with systemctl.

I already made a .service file and put that into /etc/systemd/system/. Here's the .service file:

[Unit]
Description=laporan

[Service]
ExecStart=/var/www/laporan/nodeserver/server.js
Restart=always
User=nobody
Group=root
Environment=PATH=/usr/bin:/usr/local/bin
Environment=NODE_ENV=production
WorkingDirectory=/var/www/laporan/nodeserver

[Install]
WantedBy=multi-user.target

But every time I try to start the service, it returns an error as follows (output of systemctl status laporan):

● laporan.service - laporan
   Loaded: loaded (/etc/systemd/system/laporan.service; enabled)
   Active: failed (Result: start-limit) since Mon 2016-09-12 09:15:06 WITA; 11min ago
  Process: 121690 ExecStart=/var/www/laporan/nodeserver/server.js (code=exited, status=203/EXEC)
 Main PID: 121690 (code=exited, status=203/EXEC)

Sep 12 09:15:05 kominfomdc systemd[1]: Unit laporan.service entered failed state.
Sep 12 09:15:06 kominfomdc systemd[1]: laporan.service start request repeated too quickly, refusing to start.
Sep 12 09:15:06 kominfomdc systemd[1]: Failed to start laporan.
Sep 12 09:15:06 kominfomdc systemd[1]: Unit laporan.service entered failed state.

What exactly is this error about? Am I missing something?

jkdev
  • 11,360
  • 15
  • 54
  • 77
Edgar P.
  • 129
  • 1
  • 1
  • 7

2 Answers2

18

I don't think that's how you start a node app. You are just specifying the JavaScript file here:

ExecStart=/var/www/laporan/nodeserver/server.js

You also need to specify the node executable, something like the following, if node is in the path.

ExecStart= node /var/www/laporan/nodeserver/server.js

If node isn't on the executable path, you must provide it:

ExecStart=/usr/local/bin/node /var/www/laporan/nodeserver/server.js
Robert Moskal
  • 21,737
  • 8
  • 62
  • 86
  • receive this kind of notice : Executable path is not absolute. my node is in the path as well – Edgar P. Sep 12 '16 at 02:21
  • Well, i changed it to `ExecStart=/usr/local/bin/node /var/www/laporan/nodeserver/server.js` and it run like hell. Thanks for your answer! – Edgar P. Sep 12 '16 at 02:41
  • 2
    Observe that, in some systems, node is located at /usr/bin/node, so test running full path (i.e. /usr/bin/node /dir/yourapp.js or /usr/local/bin/node /dir/yourapp.js) to see which one will work for you. – Youkko Dec 29 '16 at 17:33
  • Thanks, I had a somewhat strange issue. Node is on my path (in my EC2 instance) and running `node absolute_path_to_index.js` was running fine. However executing the exact same command in systemd was failing, apparently because systemd was using the default Node installation (it was node 8 apparently). So I ran `which node` and pasted the absolute path in systemd and this made the trick. – Christophe Vidal Oct 10 '22 at 11:33
6

to give clarity of the error you are putting the node executable path is wrong. To get the correct path you can use

$ which node // /usr/bin/node

in my case its different i get /usr/bin/node as path so in you ExecStart copy the node path from the command line

ExecStart=/usr/bin/node /var/www/laporan/nodeserver/server.js
zabusa
  • 2,520
  • 21
  • 25