How do I run a node.js app as a background service?

前端 未结 26 1552
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 05:28

Since this post has gotten a lot of attention over the years, I\'ve listed the top solutions per platform at the bottom of this post.


Original post

26条回答
  •  佛祖请我去吃肉
    2020-11-22 06:09

    June 2017 Update:
    Solution for Linux: (Red hat). Previous comments doesn't work for me. This works for me on Amazon Web Service - Red Hat 7. Hope this works for somebody out there.

    A. Create the service file 
    sudo vi /etc/systemd/system/myapp.service
    [Unit]
    Description=Your app
    After=network.target
    
    [Service]
    ExecStart=/home/ec2-user/meantodos/start.sh
    WorkingDirectory=/home/ec2-user/meantodos/
    
    [Install]
    WantedBy=multi-user.target
    

    B. Create a shell file
    /home/ec2-root/meantodos/start.sh
    #!/bin/sh -
    sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to 8080
    npm start
    
    then:
    chmod +rx /home/ec2-root/meantodos/start.sh
    (to make this file executable)
    

    C. Execute the Following
    
    sudo systemctl daemon-reload
    sudo systemctl start myapp
    sudo systemctl status myapp
    
    (If there are no errors, execute below.  Autorun after server restarted.)
    chkconfig myapp -add
    

提交回复
热议问题