Using a batch file to start pm2 when windows starts?

人走茶凉 提交于 2020-05-15 13:27:41

问题


I can't get pm2 to start my apps on Windows start. I'm running Windows Server 2012 R2 Standard and pm2 2.4.2.

I have a pm2 process file in JSON format which I use to start all my apps.

c:\pm2\process.json

{
  "apps": [
    {
      "name" : "my-app",
      "script" : "c:\\node\\myapp\index.js"
    }
  ]
}

I have a batch file which uses the JSON file:

c:\pm2\pm2-startup.bat

@echo off

set HOMEDRIVE=C:
set PM2_HOME=C:\etc\.pm2
setx /M PM2_HOME C:\etc\.pm2

cd C:\pm2 & pm2 start process.json

I have a Windows task scheduled to run the batch file:

  • Trigger: At startup
  • Run under: An administrator account
  • Run whether user is logged in or not: Yes
  • Run with highest privileges: Yes
  • Action: Start a program
    • Script: C:\pm2\pm2-startup.bat
    • Start in: C:\pm2

If I run the batch file manually (double clicking it), it works. If I run the scheduled task manually (right-click, run), it works.

When I restart the server and check the scheduled task, it has run, no errors, however the apps are not running. Doing pm2 list shows no apps in the table.

I don't want to use pm2-windows-service because I don't want to run pm2 as a service (tried it and it was flaky).

I don't want to use pm2-windows-startup either as it doesn't seem to work with a pm2 process file, it just tries to remember what was running before.

I do want to use a plain batch file on startup.

What am I doing wrong with the batch file..? Why does the scheduled task run ok, but the pm2 list is empty..?


回答1:


I know this is an old question, but I'll leave an answer for future searches.

You can put a shortcut to the .bat file in the Windows Startup folder.

Assuming the PM2 cmd is in path, your bat file could look like this.

@echo off

SET PM2_HOME=C:\Users\pstart\.pm2

pm2 start C:\path\to\run.js

That'll start PM2 and run the program. Now you just take the bat file and save it somewhere and create a shortcut in the Windows Startup folder by clicking the Start button -> Run -> shell:startup and pasting it in there.



来源:https://stackoverflow.com/questions/42929125/using-a-batch-file-to-start-pm2-when-windows-starts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!