如何监视和终止以另一个用户身份启动的进程?
How to monitor and kill processes started as another user?
有时我在一个不支持 ps -p $PID
的环境中,我希望我的用户能够发现(以可移植的 POSIX 方式)我是否以另一个用户身份启动了进程仍在 运行ning,如果时间太长也会杀死它们。
如果我的用户 运行 su anotherUser -c '/bin/sh script.sh'
并获得了 PID,我的用户 可以调用 kill -0 $PID
来查明它是否仍然是 运行宁?
或者:最便携的 POSIX 监视和终止我的用户作为另一个用户启动的进程的方法是什么?我应该 su anotherUser -c 'kill -0 $PID'
吗?
我的特定用例:编写一个库,各种内核开发人员(显然是所有 Linux,一些使用仅支持 ps
的 -w
的 BusyBox)可以作为他们的一部分一堆应用程序的平台。有时需要发生某种系统事件(如重启)。每当发生这种情况时,他们都注册了他们想要 运行 的任意脚本。但是内核方面希望确保所有这些都及时终止,因为这不应该永远持续下去。
这些注册脚本(和应用程序)运行 作为不同的用户。库 运行s 作为 root(我继承的设计),但作为拥有它们的用户启动这些脚本。因此库知道 PID,并且只有在 su username -c 'script.sh'
有效时才能成功启动脚本。
这些脚本有什么作用?我不知道,但它们都是由具有相同兴趣的内部友好人员编写的。它应该都是良性的东西,比如编写文本文件来保存应用程序状态。
(编辑:添加用例,添加缺少 ps -p
的示例环境)
不,一般来说你不能,因为kill(1)
command is doing a kill(2) syscall (see also signal(7)). See also POSIX kill function,记录在案并说
An implementation that provides extended security controls may impose further implementation-defined restrictions on the sending of signals, including the null signal. In particular, the system may deny the existence of some or all of the processes specified by pid.
但是,Linux(但不是其他 POSIX 系统)有 capabilities(7)
kill(2)
的 Linux man
页说:
For a process to have permission to send a signal it must either be privileged (under Linux: have the CAP_KILL
capability), or the real
or effective user ID of the sending process must equal the real or
saved set-user-ID of the target process. In the case of SIGCONT
it
suffices when the sending and receiving processes belong to the same
session. (Historically, the rules were different; see NOTES.)
但是,您可以使用 setuid 技术来共享真实的或保存的设置用户 ID。使用这些时要小心,错误会打开一个巨大的安全漏洞。
另请参阅 SELinux(它可能会禁止您想做的事情)。
您的最后一招 su anotherUser -c 'kill -0 $PID'
通常会奏效,但并非总是如此。坏处在于细节(想想 script.sh
运行 setuid 程序,或在 Linux 上使用功能,或某些 SELinux 或 docker 东西,。 .., 等),细节未由POSIX标准化。
也许设置一些明确的 IPC communications with your particular conventions (for the purpose of querying or killing the process, with a helper "server" or "monitoring" process), e.g. using unix(7) sockets or fifo(7)-s or pipe(7)-s 会更明智。
也许你想要一些 batch processing monitor or job scheduler, e.g. GNQS. See also Docker。
有时我在一个不支持 ps -p $PID
的环境中,我希望我的用户能够发现(以可移植的 POSIX 方式)我是否以另一个用户身份启动了进程仍在 运行ning,如果时间太长也会杀死它们。
如果我的用户 运行 su anotherUser -c '/bin/sh script.sh'
并获得了 PID,我的用户 可以调用 kill -0 $PID
来查明它是否仍然是 运行宁?
或者:最便携的 POSIX 监视和终止我的用户作为另一个用户启动的进程的方法是什么?我应该 su anotherUser -c 'kill -0 $PID'
吗?
我的特定用例:编写一个库,各种内核开发人员(显然是所有 Linux,一些使用仅支持 ps
的 -w
的 BusyBox)可以作为他们的一部分一堆应用程序的平台。有时需要发生某种系统事件(如重启)。每当发生这种情况时,他们都注册了他们想要 运行 的任意脚本。但是内核方面希望确保所有这些都及时终止,因为这不应该永远持续下去。
这些注册脚本(和应用程序)运行 作为不同的用户。库 运行s 作为 root(我继承的设计),但作为拥有它们的用户启动这些脚本。因此库知道 PID,并且只有在 su username -c 'script.sh'
有效时才能成功启动脚本。
这些脚本有什么作用?我不知道,但它们都是由具有相同兴趣的内部友好人员编写的。它应该都是良性的东西,比如编写文本文件来保存应用程序状态。
(编辑:添加用例,添加缺少 ps -p
的示例环境)
不,一般来说你不能,因为kill(1)
command is doing a kill(2) syscall (see also signal(7)). See also POSIX kill function,记录在案并说
An implementation that provides extended security controls may impose further implementation-defined restrictions on the sending of signals, including the null signal. In particular, the system may deny the existence of some or all of the processes specified by pid.
但是,Linux(但不是其他 POSIX 系统)有 capabilities(7)
kill(2)
的 Linux man
页说:
For a process to have permission to send a signal it must either be privileged (under Linux: have the
CAP_KILL
capability), or the real or effective user ID of the sending process must equal the real or saved set-user-ID of the target process. In the case ofSIGCONT
it suffices when the sending and receiving processes belong to the same session. (Historically, the rules were different; see NOTES.)
但是,您可以使用 setuid 技术来共享真实的或保存的设置用户 ID。使用这些时要小心,错误会打开一个巨大的安全漏洞。
另请参阅 SELinux(它可能会禁止您想做的事情)。
您的最后一招 su anotherUser -c 'kill -0 $PID'
通常会奏效,但并非总是如此。坏处在于细节(想想 script.sh
运行 setuid 程序,或在 Linux 上使用功能,或某些 SELinux 或 docker 东西,。 .., 等),细节未由POSIX标准化。
也许设置一些明确的 IPC communications with your particular conventions (for the purpose of querying or killing the process, with a helper "server" or "monitoring" process), e.g. using unix(7) sockets or fifo(7)-s or pipe(7)-s 会更明智。
也许你想要一些 batch processing monitor or job scheduler, e.g. GNQS. See also Docker。