一個守護進程執行的問題

本文的主線 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

Shell輸入/輸出重定向

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

  1. & makes it block on attempting to read input, and makes the shell not wait for its completion

  2. nohup 阻止SIGHUP信號發到該進程 關閉標準輸入 重定向標準輸出和標準錯誤到nohup.out

  3. 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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章