Auto start node.js server on boot

后端 未结 10 1332
隐瞒了意图╮
隐瞒了意图╮ 2020-11-30 17:16

Can any node.js experts tell me how I might configure node JS to autostart a server when my machine boots? I\'m on Windows

相关标签:
10条回答
  • 2020-11-30 17:30

    If you are using Linux, macOS or Windows pm2 is your friend. It's a process manager that handle clusters very well.

    You install it:

    npm install -g pm2
    

    Start a cluster of, for example, 3 processes:

     pm2 start app.js -i 3
    

    And make pm2 starts them at boot:

     pm2 startup
    

    It has an API, an even a monitor interface:

    AWESOME

    Go to github and read the instructions. It's easy to use and very handy. Best thing ever since forever.

    0 讨论(0)
  • 2020-11-30 17:33

    you should try this

    npm forever

    https://www.npmjs.com/package/forever

    0 讨论(0)
  • 2020-11-30 17:34

    If I'm not wrong, you can start your application using command line and thus also using a batch file. In that case it is not a very hard task to start it with Windows login.

    You just create a batch file with the following content:

    node C:\myapp.js
    

    and save it with .bat extention. Here myapp.js is your app, which in this example is located in C: drive (spcify the path).

    Now you can just throw the batch file in your startup folder which is located at C:\Users\%username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

    Just open it using %appdata% in run dailog box and locate to >Roaming>Microsoft>Windows>Start Menu>Programs>Startup

    The batch file will be executed at login time and start your node application from cmd.

    0 讨论(0)
  • 2020-11-30 17:40

    Use pm2 to start and run your nodejs processes on windows.

    Be sure to read this github discussion of how to set up task scheduler to start pm2: https://github.com/Unitech/pm2/issues/1079

    0 讨论(0)
  • 2020-11-30 17:40

    Copied directly from this answer:

    You could write a script in any language you want to automate this (even using nodejs) and then just install a shortcut to that script in the user's %appdata%\Microsoft\Windows\Start Menu\Programs\Startup folder

    0 讨论(0)
  • 2020-11-30 17:41

    I know there are multiple ways to achieve this as per solutions shared above. I haven't tried all of them but some third party services lack clarity around what are all tasks being run in the background. I have achieved this through a powershell script similar to the one mentioned as windows batch file. I have scheduled it using Windows Tasks Scheduler to run every minute. This has been quite efficient and transparent so far. The advantage I have here is that I am checking the process explicitly before starting it again. This wouldn't cause much overhead to the CPU on the server. Also you don't have to explicitly place the file into the startup folders.

    function CheckNodeService ()
    {
    
    $node = Get-Process node -ErrorAction SilentlyContinue
    
    if($node)
    {
        echo 'Node Running'
    }
    else
    {
        echo 'Node not Running'
        Start-Process "C:\Program Files\nodejs\node.exe" -ArgumentList "app.js" -WorkingDirectory "E:\MyApplication"
        echo 'Node started'
    
    }
    }
    
    CheckNodeService
    
    0 讨论(0)
提交回复
热议问题