시스템 재시작시 영구 자동 시작 (노드)
노드의 영구 모듈을 사용하여 노드 서버를 계속 실행 중입니다. 그러나 시스템을 다시 시작하면 영원히 종료됩니다. 시스템이 다시 시작될 때 노드 서버를 자동으로 시작할 수있는 방법이 있습니까?
crontab을 사용하는 것이 좋습니다. 사용하기 쉽습니다.
어떻게
편집을 시작하려면 다음을 실행하여 "testuser"를 노드 프로세스의 원하는 런타임 사용자로 바꾸십시오. 자신 이외의 다른 사용자를 선택하는 경우 sudo를 사용하여이를 실행해야합니다.
$ crontab -u testuser -e
이전에이 작업을 수행 한 적이 없다면 편집 할 편집기를 묻습니다. 나는 vim을 좋아하지만 사용하기 쉽도록 nano를 권장합니다.
편집기에서 다음 행을 추가하십시오.
@reboot /usr/local/bin/forever start /your/path/to/your/app.js
파일을 저장하십시오. cron이 설치되었다는 피드백을 받아야합니다.
cron 설치에 대한 추가 확인을 위해 다음을 실행하여 ( "testuser"를 대상 사용자 이름으로 다시 대체) 현재 설치된 크론을 나열하십시오.
$ crontab -u testuser -l
내 의견으로는 cron에서 바이너리를 실행할 때 항상 전체 경로를 사용해야한다는 점에 유의하십시오. 또한 영원한 스크립트의 경로가 올바르지 않으면 which forever
전체 경로를 얻기 위해 실행하십시오 .
해당 forever
호출이 주어지면 다음에 node
대한 전체 경로를 제공 할 수도 있습니다 node
.
@reboot /usr/local/bin/forever start -c /usr/local/bin/node /your/path/to/your/app.js
추가 자료
이를 위해 영구 서비스를 사용할 수 있습니다.
npm install -g forever-service
forever-service install test
이것은 현재 디렉토리에 app.js를 영원히 통해 서비스로 제공합니다. 시스템을 다시 시작할 때마다 서비스가 자동으로 다시 시작됩니다. 또한 중지되면 정상적으로 중지를 시도합니다. 이 스크립트는 logrotate 스크립트도 제공합니다.
깃 허브 URL : https://github.com/zapty/forever-service
참고 : 나는 영원한 서비스의 저자입니다.
이 경우는 데비안에서 유효합니다.
에 다음을 추가하십시오 /etc/rc.local
/usr/bin/sudo -u {{user}} /usr/local/bin/forever start {{app path}}
{{user}}
사용자 이름을 바꿉니다.{{app path}}
앱 경로를 대체합니다. 예를 들어/var/www/test/app.js
NPM을 사용하여 PM2를 전체적으로 설치
npm install pm2 -g
pm2로 스크립트 시작
pm2 start app.js
활성 시작 스크립트 생성
pm2 startup
참고 : pm2 시작은 시스템이 재부팅 될 때 PM2를 시작하기위한 것입니다. PM2가 시작되면 시스템이 다운되기 전에 관리했던 모든 프로세스를 다시 시작합니다.
자동 시작을 비활성화하려면 pm2 unstartup을 사용하십시오.
다른 사용자가 시작 스크립트를 실행하려면 -u <username>
옵션과--hp <user_home>:
이 답변 과이 블로그 게시물에서 영감을 얻은 대체 crontab 방법 .
1. bash 스크립트 파일을 작성하십시오 (bob을 원하는 사용자로 변경하십시오).
vi /home/bob/node_server_init.sh
2. 방금 만든 파일에 복사하여 붙여 넣습니다.
#!/bin/sh
export NODE_ENV=production
export PATH=/usr/local/bin:$PATH
forever start /node/server/path/server.js > /dev/null
설정에 따라 위의 경로를 편집하십시오!
3. bash 스크립트를 실행할 수 있는지 확인하십시오.
chmod 700 /home/bob/node_server_init.sh
4. "bob"을 노드의 런타임 사용자로 바꾸십시오.
crontab -u bob -e
5. 복사하여 붙여 넣기 (밥을 원하는 사용자로 변경).
@reboot /bin/sh /home/bob/node_server_init.sh
crontab을 저장하십시오.
당신은 그것을 끝냈습니다, 당신의상은 재부팅 (테스트) :)
첨부 된 질문 에서 답변을 복사했습니다 .
내장 된로드 밸런서가있는 Node.js 애플리케이션의 프로덕션 프로세스 관리자 인 PM2 를 사용할 수 있습니다 .
PM2 설치
$ npm install pm2 -g
응용 프로그램 시작
$ pm2 start app.js
express를 사용하면 다음과 같이 앱을 시작할 수 있습니다
pm2 start ./bin/www --name="app"
실행중인 모든 프로세스 나열 :
$ pm2 list
모든 프로세스를 나열합니다. 그런 다음 다음 명령으로 앱의 ID 또는 이름을 사용하여 서비스를 중지 / 다시 시작할 수 있습니다.
$ pm2 stop all
$ pm2 stop 0
$ pm2 restart all
로그를 표시하려면
$ pm2 logs ['all'|app_name|app_id]
You need to create a shell script in the /etc/init.d folder for that. It's sort of complicated if you never have done it but there is plenty of information on the web on init.d scripts.
Here is a sample a script that I created to run a CoffeeScript site with forever:
#!/bin/bash
#
# initd-example Node init.d
#
# chkconfig: 345
# description: Script to start a coffee script application through forever
# processname: forever/coffeescript/node
# pidfile: /var/run/forever-initd-hectorcorrea.pid
# logfile: /var/run/forever-initd-hectorcorrea.log
#
# Based on a script posted by https://gist.github.com/jinze at https://gist.github.com/3748766
#
# Source function library.
. /lib/lsb/init-functions
pidFile=/var/run/forever-initd-hectorcorrea.pid
logFile=/var/run/forever-initd-hectorcorrea.log
sourceDir=/home/hectorlinux/website
coffeeFile=app.coffee
scriptId=$sourceDir/$coffeeFile
start() {
echo "Starting $scriptId"
# This is found in the library referenced at the top of the script
start_daemon
# Start our CoffeeScript app through forever
# Notice that we change the PATH because on reboot
# the PATH does not include the path to node.
# Launching forever or coffee with a full path
# does not work unless we set the PATH.
cd $sourceDir
PATH=/usr/local/bin:$PATH
NODE_ENV=production PORT=80 forever start --pidFile $pidFile -l $logFile -a -d --sourceDir $sourceDir/ -c coffee $coffeeFile
RETVAL=$?
}
restart() {
echo -n "Restarting $scriptId"
/usr/local/bin/forever restart $scriptId
RETVAL=$?
}
stop() {
echo -n "Shutting down $scriptId"
/usr/local/bin/forever stop $scriptId
RETVAL=$?
}
status() {
echo -n "Status $scriptId"
/usr/local/bin/forever list
RETVAL=$?
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
restart
;;
*)
echo "Usage: {start|stop|status|restart}"
exit 1
;;
esac
exit $RETVAL
I had to make sure the folder and PATHs were explicitly set or available to the root user since init.d scripts are ran as root.
Use the PM2
Which is the best option to run the server production server
What are the advantages of running your application this way?
PM2 will automatically restart your application if it crashes.
PM2 will keep a log of your unhandled exceptions - in this case, in a file at /home/safeuser/.pm2/logs/app-err.log.
With one command, PM2 can ensure that any applications it manages restart when the server reboots. Basically, your node application will start as a service.
Forever was not made to get node applications running as services. The right approach is to either create an /etc/inittab entry (old linux systems) or an upstart (newer linux systems).
Here's some documentation on how to set this up as an upstart: https://github.com/cvee/node-upstart
crontab
does not work for me on CentOS x86 6.5. @reboot seems to be not working.
Finally I got this solution:
Edit: /etc/rc.local
sudo vi /etc/rc.local
Add this line to the end of the file. Change USER_NAME
and PATH_TO_PROJECT
to your own. NODE_ENV=production
means the app runs in production mode. You can add more lines if you need to run more than one node.js app.
su - USER_NAME -c "NODE_ENV=production /usr/local/bin/forever start /PATH_TO_PROJECT/app.js"
Don't set NODE_ENV
in a separate line, your app will still run in development mode, because forever does not get NODE_ENV
.
# WRONG!
su - USER_NAME -c "export NODE_ENV=production"
Save and quit vi (press ESC : w q return
). You can try rebooting your server. After your server reboots, your node.js app should run automatically, even if you don't log into any account remotely via ssh.
You'd better set NODE_ENV
environment in your shell. NODE_ENV
will be set automatically when your account USER_NAME
logs in.
echo export NODE_ENV=production >> ~/.bash_profile
So you can run commands like forever stop/start /PATH_TO_PROJECT/app.js
via ssh without setting NODE_ENV
again.
I wrote a script that does exactly this:
https://github.com/chovy/node-startup
I have not tried with forever, but you can customize the command it runs, so it should be straight forward:
/etc/init.d/node-app start
/etc/init.d/node-app restart
/etc/init.d/node-app stop
I tried lots of the above answers. None of them worked for me. My app is installed in /home
and as user, not as root. This probably means that when the above mentioned start scripts run, /home
is not mounted yet, so the app is not started.
Then I found these instructions by Digital Ocean:
Using PM2 as explained was very simple and works perfectly: My virtual servers had two physical crashes since - downtime was only about a minute.
The problem with rc.local is that the commands are accessed as root which is different than logging to as a user and using sudo.
I solved this problem by adding a .sh script with the startup commands i want to etc/profile.d. Any .sh file in profile.d will load automatically and any command will be treated as if you used the regular sudo.
The only downside to this is the specified user needs to loggin for things to start which in my situation was always the case.
complete example crontab (located at /etc/crontab) ..
#!/bin/bash
# edit this file with .. crontab -u root -e
# view this file with .. crontab -u root -l
# put your path here if it differs
PATH=/root/bin:/root/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin
# * * * * * echo "executes once every minute" > /root/deleteme
@reboot cd /root/bible-api-dbt-server; npm run forever;
@reboot cd /root/database-api-server; npm run forever;
@reboot cd /root/mailer-api-server; npm run forever;
You can use the following command in your shell to start your node forever:
forever app.js //my node script
You need to keep in mind that the server on which your app is running should always be kept on.
참고URL : https://stackoverflow.com/questions/13385029/automatically-start-forever-node-on-system-restart
'Programing' 카테고리의 다른 글
'for'루프 내에서 사후 증가 및 사전 증가는 동일한 출력을 생성합니다 (0) | 2020.05.17 |
---|---|
UDP 대 TCP, 얼마나 빠릅니까? (0) | 2020.05.17 |
IE 11 용 CSS 핵을 작성하는 방법? (0) | 2020.05.17 |
리눅스 : 백그라운드 작업 종료 (0) | 2020.05.17 |
포드 설치 -bash : 포드 : 명령을 찾을 수 없음 (0) | 2020.05.17 |