本文的主线 App => Shell => PM2 => Summary
App
vim demo.js
var http = require('http');
function random(Min, Max) {
var Range = Max - Min;
var Rand = Math.random();
return (Min + Math.round(Rand * Range));
}
var num = random(5000, 50000);
console.log(num);
http.createServer(function (req, res) {
res.end("hello node");
}).listen(num);
node demo.js
# 46165
curl localhost:46165
# hello node
Shell
- 示例1
vim demo1.sh
#! /bin/bash
node demo.js 2>&1 > demo1.txt
bash demo1.sh
tail -n 1 demo1.txt
# 35923
curl localhost:35923
# hello node
ps -ef | grep demo | grep -v grep
# ubuntu 29865 29502 0 10:07 pts/3 00:00:00 bash demo1.sh
# ubuntu 29866 29865 0 10:07 pts/3 00:00:00 node demo.js
- 示例2
vim demo2.sh
#! /bin/bash
node demo.js 2>&1 > demo2.txt &
bash demo2.sh
tail -n 1 demo2.txt
# 23357
curl localhost:23357
# hello node
ps -ef | grep demo | grep -v grep
# ubuntu 30242 1 0 10:08 pts/4 00:00:00 node demo.js
sudo kill -9 30242
PM2
- 示例1
pm2 start demo1.sh
┌─────┬──────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼──────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ demo1 │ default │ N/A │ fork │ 401 │ 0s │ 0 │ online │ 0% │ 3.0mb │ ubuntu │ disabled │
└─────┴──────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
ps -ef | grep demo | grep -v grep
# ubuntu 401 384 0 10:22 ? 00:00:00 bash /home/ubuntu/demo1.sh
# ubuntu 402 401 0 10:22 ? 00:00:00 node demo.js
pm2 delete 0
- 示例2
pm2 start demo2.sh --no-autorestart
┌─────┬──────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼──────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ demo2 │ default │ N/A │ fork │ 0 │ 0 │ 0 │ stopped │ 0% │ 0b │ ubuntu │ disabled │
└─────┴──────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
ps -ef | grep demo | grep -v grep
# ubuntu 1046 1 0 10:25 ? 00:00:00 node demo.js
pm2 delete 0 && sudo kill -9 1046
Summary
进程后台执行三要素: & | nohup(包含重定向) | disown
& makes it block on attempting to read input, and makes the shell not wait for its completion
nohup 阻止SIGHUP信号发到该进程 关闭标准输入 重定向标准输出和标准错误到nohup.out
disown 将任务从后台任务列表(jobs)移除 即不会向它发出SIGHUP信号
bash -l
echo $SHELL
# /bin/bash
shopt huponexit
# huponexit off
node demo.js &
# 14869
curl localhost:14869
# hello node
exit
ps -ef | grep demo | grep -v grep
# ubuntu 1880 1 0 10:29 pts/5 00:00:00 node demo.js
sudo kill -9 1880
bash -l
echo $SHELL
# /bin/bash
shopt huponexit
# huponexit off
shopt -s huponexit
shopt huponexit
# huponexit on
nohup node demo.js &
# nohup: ignoring input and appending output to 'nohup.out'
jobs -l
# [1]+ 10624 Running nohup node demo.js &
tail -n 1 nohup.out
# 40766
curl localhost:40766
# hello node
exit
ps -ef | grep demo | grep -v grep
bash -l
echo $SHELL
# /bin/bash
shopt huponexit
# huponexit off
shopt -s huponexit
shopt huponexit
# huponexit on
nohup node demo.js & disown
# nohup: ignoring input and appending output to 'nohup.out'
jobs -l
tail -n 1 nohup.out
# 13283
curl localhost:13283
# hello node
exit
ps -ef | grep demo | grep -v grep
# ubuntu 9671 1 0 10:59 pts/7 00:00:00 node demo.js
sudo kill -9 9671
References
有疑问加站长微信联系(非本文作者)