How does one start a node.js server as a daemon process?

后端 未结 8 1510
余生分开走
余生分开走 2020-11-30 17:41

In Python Twisted, you have the twistd command that helps you with a number of things related to running your application (daemonize it for example).

Ho

8条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 18:01

    To start a systemd service manager daemon, write a service file. For example, create a file /etc/systemd/system/myservice.service.

    [Unit]
    Description=myservice-description
    After=network.target
    
    [Service]
    ExecStart=/opt/myservice-location/src/node/server.js --args=here
    Restart=always
    User=me
    Group=group
    Environment=PATH=/usr/bin:/usr/local/bin
    Environment=NODE_ENV=production
    WorkingDirectory=/opt/myservice-location
    
    [Install]
    WantedBy=multi-user.target
    

    Remember to update the service manager daemon after every change to the myservice.service file.

    $ systemctl daemon-reload
    

    Then start the service running and enable the service to start at boot.

    $ systemctl start myservice
    $ systemctl enable myservice
    

提交回复
热议问题