Fork me on GitHub

断开ssh后继续执行--nohup

1
2
3
场景: ssh 连接开发机后启动服务,ssh 断开后服务不停
$ nohup npm start > /var/log/nginx/xxx.log 2>&1 &
运行 npm start 命令,并且 ssh 断开后继续执行。日志输出到 /var/log/nginx/xxx.log

ssh 登录阿里云 ECS,nohup 随 ssh 断开的问题

1
2
3
4
5
6
7
ssh 登录 ECS,同样是
$ nohup npm start > /var/log/nginx/xxx.log 2>&1 &
但关闭终端后进程也被杀掉了
解决方式:
$ nohup npm start > /var/log/nginx/xxx.log 2>&1 &!
$ exit
exit 退出 ssh,进程会保留,但如果 nohup 最后没有 !,exit 时会提示 zsh: you have running jobs

详情

nohup

1
2
用途: 不挂断地运行命令。
语法: nohup command [ Arg … ] [ & ]

&

1
2
用途: 在后台运行
一般两个一起用: nohup command &

输出控制台日志到文件

1
> /var/log/nginx/xxx.log 2>&1

如何查看后台运行的命令?

1
2
3
4
以上 npm start 其实是运行了一条命令 node app.js
$ ps -aux|grep node
frostbe+ 15717 0.0 3.0 661548 30884 pts/0 SNl 09:52 0:00 node app.js
进程号为 15717

重启进程

1
$ kill -usr2 15717

杀掉进程

1
kill -INT 15717
-------------感谢您的阅读 有问题请留言(或mailto:frostbelt@sina.cn)-------------