写在前面问题:

  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

使用node的pm2管理相关进程相关推荐

  1. 使用pm2启动node文件_使用 PM2 管理nodejs进程

    pm2 是一个带有负载均衡功能的Node应用的进程管理器. 当你要把你的独立代码利用全部的服务器上的所有CPU,并保证进程永远都活着,0秒的重载, PM2是完美的. 它非常适合IaaS结构,但不要把它 ...

  2. Node.js:pm2管理进程启动npm run dev和开机自启

    Node.js Production Process Manager with a built-in Load Balancer. 文档 官网 https://pm2.keymetrics.io/ g ...

  3. 使用pm2管理node服务

    最近在做项目的时候有个需求,需要将nodeJS写的接口部署到服务器上,并且要求服务器重启后该node进程也会自动启动,我在搜索相关资料后选择了使用pm2进程管理工具. pm2管理工具相关优势: (1) ...

  4. linux安装node、npm、pm2管理多个node项目同时运行

    一.安装node yum install epel-release yum install nodejs node --version 二.安装npm yum install npm npm --ve ...

  5. linux 进程管理相关命令

    进程管理相关命令 1.将前台进程放入后台,并进行查看 当用户输入一个命令并运行,就已经启动了一个进程,而且是一个前台的进程,此时系统其实已经处于一个多进程的状态(一个是 Shell 进程,另一个是新启 ...

  6. Linux中的进程管理类、ps 查看当前系统进程状态、内存置换算法LRU、查看与sshd相关进程

    文章目录 1.10 进程管理类 1.10.1 ps 查看当前系统进程状态 1.10.1.1基本语法 1.10.1.2选项说明 1.10.1.3功能说明 1.10.1.4经验技巧 1.10.1.5内存置 ...

  7. android7.0 进程管理,Android 7.0 ActivityManagerService(8) 进程管理相关流程分析(2) updateOomAdjLocked...

    前一篇博客进程管理相关流程分析(1)里, 我们介绍了AMS中updateLruProcessLocked函数相关的流程. updateLruProcessLocked只是按照进程中运行的组件,粗略地定 ...

  8. 用pm2管理一个或多个node.js应用

    0.目标 在<http-proxy反向代理以调度服务器各app> 中,我们谈到了域名解析过来后应用调度问题:除此之外,在部署了多个node.js应用后,我们还会面临多个应用管理不方便.需要 ...

  9. pm2管理koa2项目

    PM2是具有内置负载平衡器的Node.js应用程序的生产进程管理器.它可以使应用程序永远保持活动状态,可以在不停机的情况下重新加载它们,并简化常见的系统管理任务.当你要把你的独立代码利用全部的服务器上 ...

最新文章

  1. SAP WMSD集成之Copy WM Quantity
  2. ASP.NET MVC 4 (十一) Bundles和显示模式
  3. 框架:spring总结
  4. 经典的代码风格-来自微软
  5. ue的xml格式转换_VCARD格式
  6. 返回局部变量或临时变量的地址_C++的函数不可以返回局部变量的指针
  7. 云鲸扫拖一体机器人说明书_云鲸扫拖一体机器人开箱测评。拖地机器人的天花板是什么样的?...
  8. kafka重复消费问题
  9. 四则运算题目生成程序(基于控制台)
  10. 面向对象之编写一个完整的类
  11. 图论 —— 图的连通性 —— 并查集判断连通性
  12. 分布式红锁的waitTime的设计原理
  13. 联想服务器安装2019系统,联想支持的Win10 2019年10月更新(1909版本)的机型
  14. 使用DWR出现“例外被抛出且未被接住”错误的原因和解决办法
  15. 生成扩散模型漫谈:DDPM = 自回归式VAE
  16. linux shell 端口扫描,shell脚本结合iptables防端口扫描的实现
  17. Java并发编程之ThreadPoolExecutor源码解析
  18. 判断当前入口是PC端企业微信还是PC端浏览器。或者是APP端企业微信
  19. 电脑麦克风没声音怎么办?3个方法快速解决
  20. 快排算法的针对重复键值的优化

热门文章

  1. 【java web】java执行预编译Groovy脚本
  2. 1218 标签的显示与隐藏
  3. 分组框控件 1130
  4. python-函数的定义与调用
  5. python-数据类型-整数类型与浮点数据类型
  6. gem5的安装、编译及运行
  7. create-react-app教程-源码篇
  8. 如何在Ubuntu 18.04上安装Django
  9. BZOJ 1032 JSOI 2007 祖码Zuma 区间DP
  10. .NET中Redis安装部署及使用方法简介附-开源Redis操作辅助类