Windows启动时使用批处理文件启动pm2吗? [英] Using a batch file to start pm2 when windows starts?

查看:876
本文介绍了Windows启动时使用批处理文件启动pm2吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法 pm2 在Windows启动时启动我的应用.我正在运行Windows Server 2012 R2 Standard和pm2 2.4.2.

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.

我有一个使用JSON格式的 pm2进程文件启动我所有的应用程序.

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"
    }
  ]
}

我有一个使用JSON文件的批处理文件:

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

我安排了Windows任务来运行批处理文件:

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

  • 触发:启动时
  • 运行于:管理员帐户
  • 运行是否登录用户:
  • 以最高特权运行:
  • 操作:启动程序
    • 脚本: C:\ pm2 \ pm2-startup.bat
    • 开始于: C:\ pm2
    • 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.

      当我重新启动服务器并检查计划的任务时,它已运行,没有错误,但是应用程序未运行.进行pm2 list时表中没有显示任何应用程序.

      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.

      我不想使用 pm2-windows -service ,因为我不想将pm2作为服务运行(尝试过并且它很不稳定).

      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).

      我不想使用 pm2-windows -startup ,因为它似乎不适用于 pm2进程文件,它只是想记住之前在运行什么.

      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.

      批处理文件我在做什么错?.?为什么计划的任务运行正常,但是pm2列表为空..?

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

      推荐答案

      我知道这是一个老问题,但是我将为以后的搜索留下答案.

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

      您可以在Windows启动文件夹中放置.bat文件的快捷方式.

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

      假设PM2 cmd在路径中,则您的蝙蝠文件可能如下所示.

      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
      

      这将启动PM2并运行程序.现在,您只需将bat文件保存到某个位置,然后通过单击开始"按钮->运行-> shell:startup并将其粘贴到Windows Startup文件夹中创建快捷方式即可.

      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.

      这篇关于Windows启动时使用批处理文件启动pm2吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆