大网数据广告活动
服务器运维问答

Process terminates after SSH logout on windows server

· 52CE 我爱测

I have a Windows server 2016 machine that needs to run an file that uses the cli, remotely using SSH . It is known that logging out of an SSH connection terminates processes that were started by the ssh user. On linux machines, o…

问题原文

原帖链接 · Pizza · CC BY-SA 4.0

I have a Windows server 2016 machine that needs to run an file that uses the cli, remotely using SSH.

It is known that logging out of an SSH connection terminates processes that were started by the ssh user. On linux machines, one may use solutions such as nohup or screen, resulting a process not being killed once logging off.

Such solutions won't work on windows machines though, and I'm looking for a way to prevent a process from getting terminated once logged out.

  • My client is an Ubuntu machine
  • My Windows server 2016 runs OpenSHH, with PowerShell as a default shell
  • My program is set to work in daemon mode, and no cmd or powershell windows are left open
  • Using Get-Process -Name proc_name shows the process is indeed running
  • Using Get-Process after SSH logout yields no such process

Things I also tried:

  • Running the .exe file using cmd.
  • Running a .bat and .ps files that starts the .exe with its arguments.

Is there a way to tackles this problem on a windows machine?

已采纳解答

原帖链接 · Pizza · CC BY-SA 4.0

After spending much time digging for solutions, I found the absolute correct way of doing so using PowerShell.

It seems like it is one of those cmdlets less used or documented online, and is almost impossible to figure out on your own using the MSDN documentation, unless you're already very familiar with the huge world of Wmi Objects.

In short:

Assuming I have a program foo.exe that needs to run on background with the arguments -a, -b and bar, I should be using the exact command:

Invoke-WmiMethod -Path 'Win32_Process' -Name Create -ArgumentList 'C:\Users\foo\Desktop\foo.exe -a -b bar'

Of course if your exe is a "known" exe (either by default, such as notepad or ping) or added by you, a full path is not needed when using the -ArgumentList and exe name would be sufficient (notepad, ping, and etc..).

Additional parameters:

Invoke-WmiMethod supports additional parameters such as -Credential, -ComputerName (a good way to use a local PS to Invoke something on a remote machine without using SSH) , -Impersonation and many more documented here.

Cmdlet syntax explanation and additional cmdlet tools:

  • The -Path argument directs to a WmiObject name. There are dozens of WmiObjects, each with many Methods and Properties.
  • The -Name argument directs the used Method.
  • To list all available Wmi-Objects use Get-WmiObject -List (very difficult to locate your needed object this way).
  • To list and view all available Win32_Process Methods and Properties use
Get-WmiObject -List |where{$_.name -match '^Win32_Process$'}

This will return an output of this structure:

   NameSpace: ROOT\cimv2

Name                                Methods              Properties
----                                -------              ----------
Win32_Process                       {Create, Terminat... {Caption, CommandLine, CreationClassName, CreationDate...}

And of course using |Select-Object -ExpandProperty (_your_property_here_) will reveal all available Methods such as Create we used in the command above, and all available Properties (which we did not use).

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-11-30)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。