Programing

부팅시 node.js 서버 자동 시작

lottogame 2020. 10. 28. 07:36
반응형

부팅시 node.js 서버 자동 시작


node.js 전문가가 내 컴퓨터가 부팅 될 때 서버를 자동 시작하도록 노드 JS를 구성하는 방법을 알려줄 수 있습니까? Windows를 사용 중입니다.


이것은 node.js에서 구성 할 사항이 아니며 순전히 OS 책임입니다 (귀하의 경우 Windows). 이를 달성하는 가장 안정적인 방법은 Windows 서비스를 사용하는 것입니다.

노드 스크립트를 Windows 서비스로 설치하는 매우 쉬운 모듈 이 있는데 ,이를 node-windows ( npm , github , documentation )라고합니다. 예전에 사용 해본 적이 있고 매력처럼 일 했어요.

var Service = require('node-windows').Service;

// Create a new service object
var svc = new Service({
  name:'Hello World',
  description: 'The nodejs.org example web server.',
  script: 'C:\\path\\to\\helloworld.js'
});

// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
  svc.start();
});

svc.install();

추신

나는 그것이 매우 유용하다는 것을 발견하여 그 주위에 사용하기 더 쉬운 래퍼 ( npm , github ) 를 만들었습니다 .

설치 :

npm install -g qckwinsvc

서비스 설치 :

> qckwinsvc
prompt: Service name: [name for your service]
prompt: Service description: [description for it]
prompt: Node script path: [path of your node script]
Service installed

서비스 제거 :

> qckwinsvc --uninstall
prompt: Service name: [name of your service]
prompt: Node script path: [path of your node script]
Service stopped
Service uninstalled

Linux를 사용하는 경우 macOS 또는 Windows pm2 가 친구입니다. 클러스터를 매우 잘 처리하는 프로세스 관리자입니다.

당신은 그것을 설치합니다 :

npm install -g pm2

예를 들어, 3 개의 프로세스로 구성된 클러스터를 시작하십시오.

 pm2 start app.js -i 3

그리고 pm2는 부팅시 그들을 시작합니다 :

 pm2 startup

API, 모니터 인터페이스도 있습니다 .

대박

github로 이동하여 지침을 읽으십시오 . 사용하기 쉽고 매우 편리합니다. 영원히 최고의 것 .


내가 틀리지 않은 경우 명령 줄을 사용하여 배치 파일을 사용하여 응용 프로그램을 시작할 수 있습니다. 이 경우 Windows 로그인으로 시작하는 것은 그리 어려운 작업이 아닙니다.

다음 내용으로 배치 파일을 생성하기 만하면됩니다.

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.


I would recommend installing your node.js app as a Windows service, and then set the service to run at startup. That should make it a bit easier to control the startup action by using the Windows Services snapin rather than having to add or remove batch files in the Startup folder.

Another service-related question in Stackoverflow provided a couple of (apprently) really good options. Check out How to install node.js as a Windows Service. node-windows looks really promising to me. As an aside, I used similar tools for Java apps that needed to run as services. It made my life a whole lot easier. Hope this helps.


you should try this

npm forever

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


This can easily be done manually with the Windows Task Scheduler.

  • First, install forever.
  • Then, create a batch file that contains the following:

    cd C:\path\to\project\root
    call C:\Users\Username\AppData\Roaming\npm\forever.cmd start server.js
    exit 0
    
  • Lastly, create a scheduled task that runs when you log on. This task should call the batch file.


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


Here is another solution I wrote in C# to auto startup native node server or pm2 server on Windows.


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

Copied directly from this answer:

이를 자동화하려는 모든 언어로 스크립트를 작성한 다음 (nodejs를 사용하더라도) 사용자의 % appdata % \ Microsoft \ Windows \ Start Menu \ Programs \ Startup 폴더에 해당 스크립트의 바로 가기를 설치할 수 있습니다.

참고 URL : https://stackoverflow.com/questions/20445599/auto-start-node-js-server-on-boot

반응형