为什么进程被 nohup 杀死

Yus*_*eri 7 nohup background-process uwsgi

我想在后台运行一个进程而不在 shell 退出时杀死它,根据 Nohup 概念,以下命令应该可以工作,直到我手动杀死它:

nohup uwsgi --http :8008  --module crawled_data_center.wsgi > /dev/null &
Run Code Online (Sandbox Code Playgroud)

我使用 root 用户登录到 shell,但是在退出 shell 后,进程终止了。这看起来很奇怪,因为我nohup在几个项目中使用了几次并且工作正常,但在这种情况下我很糟糕,有什么问题,我怎样才能在后台运行它而不在 shell 退出时杀死它?

更新:

我处理它:

$ nohup uwsgi --http :8008  --module crawled_data_center.wsgi > /dev/null &
$ disown -l
$ disown -h JOBID
Run Code Online (Sandbox Code Playgroud)

但我的问题是关于它怎么SIGHUP可能杀死nohup&

以下是内容/etc/systemd/logind.conf

[Login]
#NAutoVTs=6
#ReserveVT=6
#KillUserProcesses=no
#KillOnlyUsers=
#KillExcludeUsers=root
Controllers=blkio cpu cpuacct cpuset devices freezer hugetlb memory perf_event net_cls net_prio
ResetControllers=
#InhibitDelayMaxSec=5
#HandlePowerKey=poweroff
#HandleSuspendKey=suspend
#HandleHibernateKey=hibernate
#HandleLidSwitch=suspend
#PowerKeyIgnoreInhibited=no
#SuspendKeyIgnoreInhibited=no
#HibernateKeyIgnoreInhibited=no
#LidSwitchIgnoreInhibited=yes
#IdleAction=ignore
#IdleActionSec=30min
Run Code Online (Sandbox Code Playgroud)

Fox*_*Fox 22

据我所知,有两种情况会导致进程被保护后被杀死nohup,每种情况都有不同的解决方法。

一种可能性(此处似乎并非如此)是系统使用 systemd,其中logind.conf配置了KillUserProcesses=yes. 在这种情况下,关闭终端不会引起问题,但退出系统会引起问题。在这种情况下的解决方法是使用

$ systemd-run --scope --user [command]
Run Code Online (Sandbox Code Playgroud)

这基本上只是告诉 systemd 它不应该终止进程。

另一种可能性是生成的进程实现了自己的处理程序,SIGHUP该处理程序覆盖了nohup. 在这种情况下,即使 shell 关闭,问题也会出现,即使您保持登录状态。您可以通过以下方式检查:

$ nohup [command] &
$ grep Sig /proc/$!/status
Run Code Online (Sandbox Code Playgroud)

你应该看到一条线

SigIgn: 0000000000000001
Run Code Online (Sandbox Code Playgroud)

(或其他一些十六进制数字字符串)。SIGHUP是信号号1,所以如果这个大端十六进制数设置了它的第一个(最低有效)位(即最后一个数字是 1、3、5、7、9、B、D 或 F 之一),然后SIGHUP被忽略。否则,程序已经安装了自己的处理程序,覆盖了nohup.

在这种情况下,解决方案是使用disown

nohup [command] & disown
Run Code Online (Sandbox Code Playgroud)

这将从外壳程序的作业列表中删除该进程,从而防止SIGHUP首先发送。