注销时 bash 如何处理作业?
据我从书籍和 bash 手册中了解到的就是这样。当用户从 bash 注销时,如果用户没有使用 nohup 或 disown,则该用户启动的所有后台作业都将自动终止。但今天我测试了它:
- 登录到我的 gnome 桌面并访问 gnome-terminal。
终端中有两个选项卡,在一个选项卡中我创建了一个名为 test 的新用户并以 test 身份登录
su - 测试
身份登录并启动了一个脚本。
cat test.sh #!/bin/bash 睡眠 60 printf“你好世界!!” 出口0 ./test.sh &
之后,我退出测试并关闭选项卡
- 在下一个选项卡中,我以 root 身份执行 ps aux ,发现作业仍在运行。
这是怎么回事?
As far as I understood from the books and bash manuals is that. When a user logs out from bash all the background jobs that is started by the user will automatically terminate, if he is not using nohup or disown. But today I tested it :
- Logged in to my gnome desktop and accessed gnome-terminal.
There are two tabs in the terminal and in one I created a new user called test and logged in as test
su - test
started a script.
cat test.sh #!/bin/bash sleep 60 printf "hello world!!" exit 0 ./test.sh &
After that I logged out of test and closed the tab
- In the next tab I exected ps aux as root and found that job is still running.
How this is happening ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正在运行的后台作业是否在退出时终止取决于 shell。 Bash 通常不会执行此操作,但可以配置为登录 shell (
shopt -s huponexit
)。无论如何,在控制进程(例如登录 shell)终止后,就不可能访问 tty。总是导致
SIGHUP
的情况包括:SIGCONT
和SIGHUP
)。 shell 通常会在发生这种情况之前警告您。huponexit 摘要:
开启:当 shell 退出时,后台作业将通过 SIGHUP 终止
关:当 shell 退出时,后台作业不会通过 SIGHUP 终止.
Whether running background jobs are terminated on exit depends on the shell. Bash normally does not do this, but can be configured to for login shells (
shopt -s huponexit
). In any case, access to the tty is impossible after the controlling process (such as a login shell) has terminated.Situations that do always cause
SIGHUP
include:SIGCONT
andSIGHUP
). Shells typically warn you before letting this happen.huponexit summary:
On: Background jobs will be terminated with SIGHUP when shell exits
Off: Background jobs will NOT be terminated with SIGHUP when shell exits.
只有交互式 shell 才会在您关闭作业时将其杀死。其他 shell(例如通过使用
su - username
获得的 shell)不会执行此操作。交互式 shell 只会杀死直接子进程。Only interactive shells kill jobs when you close them. Other shells (for example those you get by using
su - username
) don't do that. And interactive shells only kill direct subprocesses.