在远程会话中启动后台任务,当会话被删除时,这些后台任务不会被终止
我一直在使用 PsExec -d 在远程 powershell 会话中启动控制台应用程序,因为我希望这些应用程序在我执行某些任务时在后台运行。问题是,即使我使用 Remove-PSSession
终止远程 powershell 会话,我也希望后台应用程序继续运行。当前发生的情况是,一旦远程 powershell 会话被终止,所有在 PsExec -d 帮助下启动的进程也会被终止。我猜这与进程树以及 Windows 如何管理此类事物的生命周期有关。
有谁知道如何启动远程后台进程并让该进程即使在远程会话被终止后仍然存在?
I have been using PsExec -d
to launch console applications in a remote powershell session because I want these apps to run in the background while I perform some task. The problem is that I want the background applications to continue running even if I kill the remote powershell session with Remove-PSSession
. What happens currently is once the remote powershell session is killed so are all the processes that were started with the help of PsExec -d
. I'm guessing it has something to do with process trees and how windows manages the lifetime of such things.
Does anyone have any idea how I can launch a remote background process and have that process persist even after the remote session is killed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先解释一下它为什么如此工作。也许其他人可以用它来带来另一种解决方案。
我使用基于 WMI 的解决方案编辑了我的答案。
当您进入远程会话时:
您在服务器上创建一个名为
wsmprovhost.exe
的进程,如当您仅在此远程会话中启动进程时:
下所示
新进程是
wsmprovhost.exe
的子进程,如如果停止远程会话,
wsmprovhost.exe
就会消失,子进程也会消失。解释是
wsmprovhost.exe
和由此启动的所有进程属于同一个作业。默认情况下,一方面此作业不支持
JOB_OBJECT_LIMIT_BREAKAWAY_OK
限制标志不允许我们使用CREATE_BREAKAWAY_FROM_JOB
标志启动进程,但另一方面,此作业支持JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
限制标志,导致与作业关联的所有进程在作业的最后一个句柄关闭时终止。也许存在一种解决方案来配置 WinRM 以支持支持
JOB_OBJECT_LIMIT_BREAKAWAY_OK
的作业。编辑:
所以阅读 Microsoft 文档< /a>,我找到了一种记录在案的技术方法,供您通过 WinRM 启动程序,但在另一项工作中。默认情况下,由与作业关联的进程使用 CreateProcess 创建的进程与该作业关联;但是,使用Win32_Process.Create创建的进程与作业无关。
因此,如果在远程会话中您使用 WMI 创建一个进程,如下所示:
如果您停止远程会话,wsmprovhost.exe 就会消失,但新进程仍保留在服务器上,如下所示:
使用 WMI 启动的进程不属于任何作业。用法语我会说“Ce qu'il Fallait démontrer”
Here is first an explanation of why it works so. Perhaps someone else can use it to bring a another solution.
I edited my answer with solution based on WMI.
When you enter a remote session :
You create on the server a process called
wsmprovhost.exe
as shown here underWhen you simply start a process in this remote session :
The new process is a child of
wsmprovhost.exe
as shown here underIf you stop the remote session
wsmprovhost.exe
disappeared and so the child process.The explanation is that
wsmprovhost.exe
and all the processes started by this one belongs to the same job.By default, on one hand this job DOES NOT supports
JOB_OBJECT_LIMIT_BREAKAWAY_OK
limit flag that does not allow us to start a process withCREATE_BREAKAWAY_FROM_JOB
flag, on the other hand this job supportsJOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
limit flag that causes all processes associated with the job to terminate when the last handle to the job is closed.It perhaps exists a solution to configure WinRM to support jobs which supports
JOB_OBJECT_LIMIT_BREAKAWAY_OK
.Edited :
So reading Microsoft documentation, I found a documented technical way for you to start a program through WinRM but in an onother job. By default, processes created using CreateProcess by a process associated with a job are associated with the job; however, processes created using Win32_Process.Create are not associated with the job.
So if in you remote session you create a process with WMI like this :
If you stop the remote session wsmprovhost.exe disappeared, but the new process stay on the server as show here under :
The processes started with WMI does not belongs to any Job. In french I would say "Ce qu'il fallait démontrer"