Create systemd nodejs express-generator

I have a nodejs server that is generated by an express generator. Now I am trying to create a systemd service to start my server.

[Unit] Description=Node.js Example Server [Service] ExecStart=/usr/bin/node /opt/nodeserver/server.js Restart=always StandardOutput=syslog StandardError=syslog SyslogIdentifier=nodejs-example Environment=NODE_ENV=production PORT=1337 [Install] WantedBy=multi-user.target 

The service cannot start because I cannot start the server using the node server.js command. The only way to start the server: npm server.js. How can I say that the service starts the server using the npm command and not the node?

0
express
Nov 29 '16 at 8:42 on
source share
1 answer

Systemd can be problematic sometimes. It's easier to write SysV or Upstart initialization scripts every time you can. I will explain some of these questions: [1] , [2] .

Now, what you can do is write the script itself, which:

  • change the working directory to your application
  • run the run command

For example:

 #!/bin/sh cd /your/application/path npm start 

(or npm server.js or whatever your command is instead of npm start )

It may also be useful to set the correct PATH if you need it:

 #!/bin/sh PATH="/opt/node/bin:$PATH" export PATH cd /your/application/path npm start 

(where /opt/node/bin is your node and npm )

If you save a script like /opt/nodeserver/server.sh , for example, then you should be able to use:

 ExecStart=/bin/sh /opt/nodeserver/server.sh 

on your systemd script (if there are no more gotchas in systemd that could cause problems here).

0
Nov 29 '16 at 10:35
source share
— -



All Articles