How to run node.js app forever when console is closed?

前端 未结 12 625
遇见更好的自我
遇见更好的自我 2020-12-12 09:38

I connect to my remote server via ssh. Then I start my node.js app with Forever. Everything works fine until I close my console window. How to run node.js app FOREVER on my

相关标签:
12条回答
  • 2020-12-12 10:08

    I would definitely recommend pm2
    npm install -g pm2

    To start server: pm2 start [yourServerFile.js]
    To stop server: pm2 stop [yourServerFile.js]

    Close client and server will run forever....will also restart if app crashes.
    Ive been running a node server on Ubuntu for months with zero issues

    0 讨论(0)
  • 2020-12-12 10:10

    You could install forever using npm like this:

      sudo npm install -g forever
    

    Or as a service:

     forever start server.js
    

    Or stop service

    forever stop server.js
    

    To list all running processes:

    forever list
    
    0 讨论(0)
  • 2020-12-12 10:11

    node expamle.js & for example

    0 讨论(0)
  • 2020-12-12 10:12

    my start.sh file:

    #/bin/bash
    
    nohup forever -c php artisan your:command >>storage/logs/yourcommand.log 2>&1 &
    

    There is one important thing only. FIRST COMMAND MUST BE "nohup", second command must be "forever" and "-c" parameter is forever's param, "2>&1 &" area is for "nohup". After running this line then you can logout from your terminal, relogin and run "forever restartall" voilaa... You can restart and you can be sure that if script halts then forever will restart it.

    I <3 forever

    0 讨论(0)
  • 2020-12-12 10:13

    I had similar issue and I think using forever will help to handle crashed and restarts

    You can install forever globally: sudo nom install -g forever

    And run this command:

    nohup forever server.js &
    

    This should handle all the trouble of closing the terminal, closing ssh session, node crashes and restarts.

    0 讨论(0)
  • 2020-12-12 10:18

    In Linux, SSH into your remote server and run

    screen
    

    to launch into a new screen.

    Finally, type ctrlad to detach the screen session without killing the process.

    More info here.

    0 讨论(0)
提交回复
热议问题