Running a Node.js script 24/7 on a Windows VPS (auto-start and auto-restart)

trixdev

Estimable Member
VIP
Member
Joined
Threads
19
Posts
22
Keep your bot or script running after you log out

Closing the RDP window or rebooting kills a script started in a console. Here are three ways to keep it running, from simplest to most robust.

Option 1: Task Scheduler
Create a task with the trigger "At startup". Set the action to start node.exe with your script as the argument. On the Settings tab, enable "If the task fails, restart every 1 minute". This needs nothing extra installed.

Option 2: NSSM (runs it as a Windows service)
Code:
nssm install MyBot "C:\Program Files\nodejs\node.exe" "C:\bots\app.js"
nssm set MyBot AppDirectory C:\bots
nssm set MyBot AppStdout C:\bots\out.log
nssm set MyBot AppStderr C:\bots\err.log
nssm start MyBot
It starts before anyone logs in and restarts the script if it crashes.

Option 3: pm2
Code:
npm i -g pm2
pm2 start app.js --name mybot
pm2 save
pm2 is great for logs and monitoring. Auto-start at boot on Windows needs an extra helper package, so I'd use NSSM for the boot part.

You must reply to see the hidden content. Consider upgrading your account to increase your reply limit.
 
  • trixdev
    Created
  • Last reply
  • 0
    Replies
  • 304
    Views
  • 1
    Participants
  • Participants list