使用node的pm2管理相关进程

 2023-09-06 阅读 26 评论 0

摘要:写在前面问题: 如何开机自启动?如何自动拉起挂掉的进程 ?如何做到session关掉后session的子进程还正常运行? 基础知识学习 请先阅读 http://www.ruanyifeng.com/blog/2016/02/linux-daemon.html shopt | grep huponexit SIGHUP信号,比如关闭一个终

写在前面问题:

  1. 如何开机自启动?
  2. 如何自动拉起挂掉的进程 ?
  3. 如何做到session关掉后session的子进程还正常运行?

基础知识学习

请先阅读
http://www.ruanyifeng.com/blog/2016/02/linux-daemon.html
shopt | grep huponexit
SIGHUP信号,比如关闭一个终端,或者关闭一个进程,都可以通过发送SIGHUP信号
vi test.sh & 创建了一个后台的进程
jobs 查看后台进程
nohup 命令做了以下的事情

 nohup的作用
阻止SIGHUP信号发到这个进程。
关闭标准输入。该进程不再能够接收任何输入,即使运行在前台。
重定向标准输出和标准错误到文件nohup.out。

什么是pm2

类比理解:pm2可以类比于supervisord
pm2是一个用node编写的进程管理工具,它不仅可以用来管理node进程,任何进程相关的东西都可以使用pm2来进行管理
安装pm2, npm install pm2

pm2 start app.js 开启进程
文件后缀会对应相应的解释器,当然你也可以自己指定解释器

{".sh": "bash",".py": "python",".rb": "ruby",".coffee" : "coffee",".php": "php",".pl" : "perl",".js" : "node"
}

pm2 list 查看当前被pm2所管理的进程
在这里插入图片描述
pm2 log查看日志
pm2 stop all 关闭所有进程

pm2 reload app_name 重新加载进程
pm2 monit 本地监控
pm2 delete app_name 删除进程

开机自启

开机自启意味着一旦服务器重新启动了,我们的程序不需要人工介入就可以自动进行启动,这对于部署在本地服务器上的脚本来说非常的有用。
不同的操作系统提供了不同的操作系统开机自启的方式,pm2对此进行了封装,使用pm2可以无需关注底层细节,简洁而方便的创建开机自启程序。

pm2 startup
pm2 save

进程管理

linux系统中的service或systemd其实就是进程管理最好的例子。
大前端工程师往往对后端不太了解,他们启动一个进程,有时候就是在命令行执行一下,有点计算机基础的会使用nohup,但这里已经到他们的极致了,要让一个进程能够正常工作是要做很多事情的,生命周期的管理,进程挂掉后自动拉起,开机后的自我恢复,等等。

其他进程管理工具

supervisord

forever

nodemon

开发环境使用,一旦文件发生变化,自动重启进程

systemd

大部分系统默认的进程管理工具
下面这篇文章讲解了使用systemd启动一个node应用
https://www.axllent.org/docs/view/nodejs-service-with-systemd/
Node.js as a running service is becoming more and more popular these days. One of the issues many developers face is how to ensure their node.js service starts automatically, and more importantly how to keep it running should it crash. Previously one had to install modules such as forever, and then create some autostart script to start the daemon when the server booted.

Most Linux systems have recently switched to using systemd, which makes this process a lot simpler and more efficient, and means that we do not need forever any more.

Create the node.js server
For this example we will use a slightly modified “web server” of the example from the Node.js website:

const http = require(‘http’);

const hostname = ‘0.0.0.0’; // listen on all ports
const port = 1337;

http.createServer((req, res) => {
res.writeHead(200, { ‘Content-Type’: ‘text/plain’ });
res.end(‘Hello World\n’);
}).listen(port, hostname, () => {
console.log(‘Server running at http://hostname:{hostname}:hostname:{port}/’);
});
We will save this file (for example) as /opt/nodeserver/server.js. Verify the node server works

node /opt/nodeserver/server.js
Server running at http://0.0.0.0:1337/
Assuming it works, we will now create a systemd file to start this server as a service.

Systemd
Create the service file
Create /etc/systemd/system/nodeserver.service

[Unit]
Description=Node.js Example Server
#Requires=After=mysql.service # Requires the mysql service to run first

[Service]
ExecStart=/usr/local/bin/node /opt/nodeserver/server.js

Required on some systems

#WorkingDirectory=/opt/nodeserver
Restart=always

Restart service after 10 seconds if node service crashes

RestartSec=10

Output to syslog

StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodejs-example
#User=
#Group=
Environment=NODE_ENV=production PORT=1337

[Install]
WantedBy=multi-user.target
Enable the service
systemctl enable nodeserver.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nodeserver.service to /etc/systemd/system/nodeserver.service.
Start the service
systemctl start nodeserver.service
Verify it is running
systemctl status nodeserver.service
● nodeserver.service - Node.js Example Server
Loaded: loaded (/etc/systemd/system/nodeserver.service; enabled)
Active: active (running) since Thu 2015-12-31 09:29:35 NZDT; 7s ago
Main PID: 8952 (node)
CGroup: /system.slice/nodeserver.service
└─8952 /usr/local/bin/node /opt/nodeserver/server.js

Dec 31 09:29:35 fileserver nodejs-example[8952]: Server running at http://0.0.0.0:1337/
Security / testing
Of course this service would run as root, which you probably shouldn’t be doing, so you can edit /etc/systemd/system/nodeserver.service to run it as a different user depending on your system. If you make any changes to the service file, you will need to do a

systemctl daemon-reload
before reloading the service:

systemctl restart nodeserver.service
I always suggest doing a systemctl status nodeserver.service after edits and a restart to ensure the service is running as expected.

Now finally we can test how systemd restarts the process if it unexpectedly dies (thanks Mark for the suggestion). We will manually “kill” the running process by finding its Process ID (pid), and note how systemd automatically restart it again:

ps -ef | grep server.js # find the current pid
kill 12345 # kill the process by its pid reported above
ps -ef | grep server.js # notice node process is immediately respawned with a different pid


但是每次在用脚本语言的时候,心里总是慌慌的。
比如下面三个程序,哪个给你可靠的感觉?我觉得还是java可靠,真是奇怪啊。

  1. java -jar dosomething.jar
  2. python dosomething.py
  3. node dosomething.js

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/6832.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息