Dealing with SIGHUPs
不管一个进程是后台运行的还是前台运行的,它都和启动他的terminal紧密联系在一起,如果这个ternimal关闭了,它就会给所有它启动的进程发送一个 SIGHU信号,然后所有的进程都挂掉了(和设置有关系哈,不是绝对的),但是,如果我们希望我们的进程在terminal关闭的时候仍然继续运行怎么办?
There are a number of ways of accomplishing this. The most flexible ways are typically to use a terminal multiplexer like screen
or tmux
, or use a utility that provides at least the detach functionality of those, like dtach
.
我们有多种办法来实现这个需求,比如使用screen,即使你关闭了terminal,screen内的进程仍然会正常运行,screen就像是一个隔离罩
使用nohup
Nohup的作用就是忽略SIGHUP信号,即使terminal关了,信号发过来了,nohup 会忽略掉
举个例子:
- nohup ping -i 5 google.com &
输出:
Output
1 2 |
nohup: ignoring input and appending output to ‘nohup.out’ |
这个时候我们的输出会被重定向到nohup.out(默认当前目录,当前目录不可写的话,跑到home目录)
注意,这个时候job 命令不会显示,因为nohup用的是自己独立的job队列
如何杀掉呢?先找到它
- pgrep -a ping
输出
1 2 |
7360 ping -i 5 google.com |
然后根据PID干掉:
- kill 7360
通过修改shell的参数
这就是我们上边说的特殊情况了,我们配置了shell的参数以后,即使推出,后台运行的程序也不会结束
如何查看当前设置:
- shopt huponexit
打开::
- shopt -s huponexit
这个时候如果你通过输入命令exit退出,你的程序会继续完美运行:
- exit
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021
广州网站建设 2018/08/07 10:00
打卡