一个守护进程执行的问题


本文摘自网络,作者,侵删。

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

  • How do I get my Golang web server to run in the background?

  • Difference between nohup, disown and &

  • Difference between nohup and &(ampersand) in linux ?

  • shopt command not found in .bashrc after shell updation

  • Linux守护进程的启动方法


本文来自:简书

感谢作者:诺之林

查看原文:一个守护进程执行的问题

相关阅读 >>

Golang中...是什么意思?

Go Go.mod详解

Golang配置私有仓库Go get

有趣的闭包

研究数组

Golang中将字节流转为protobuf

详解 Go 语言中的方法

Go 获取 html 标签中的文本_关于html中progress标签的定义及用法汇总!

Golang基础-http server

如何使用Go优雅地撰写单元测试

更多相关阅读请进入《Go》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...