我正在尝试使用TProcess单元执行ssh以连接到我的一台服务器并为我提供 shell 。这是我在Ruby中的一个重写,因为Ruby的执行时间非常慢。当我运行Process.Execute函数时,会看到shell,但它会立即背景化。运行pgrep ssh
显示它正在运行,但是我无权访问它,使用fg
不会将其恢复。该段的代码如下:
if HasOption('c', 'connect') then begin
TempFile:= GetRecord(GetOptionValue('c', 'connect'));
AProcess:= TProcess.Create(nil);
AProcess.Executable:= '/usr/bin/ssh';
AProcess.Parameters.Add('-p');
AProcess.Parameters.Add(TempFile.Port);
AProcess.Parameters.Add('-ntt');
AProcess.Parameters.Add(TempFile.Username + '@' + TempFile.Address);
AProcess.Options:= [];
AProcess.ShowWindow:= swoShow;
AProcess.InheritHandles:= False;
AProcess.Execute;
AProcess.Free;
Terminate;
Exit;
end;
TempFile
是TProfile
类型的变量,它是包含有关服务器信息的记录。编目系统和检索工作正常,但拉起 shell 则不行。
最佳答案
...
AProcess.ShowWindow:= swoShow;
AProcess.InheritHandles:= False;
AProcess.Execute;
AProcess.Free;
...
您正在启动该过程,但不等待它退出。这是从the documentation on Execute
:Execute actually executes the program as specified in CommandLine, applying as much as of the specified options as supported on the current platform.
If the poWaitOnExit option is specified in Options, then the call will only return when the program has finished executing (or if an error occured). If this option is not given, the call returns immediatly[sic], but the WaitOnExit call can be used to wait for it to close, or the Running call can be used to check whether it is still running.
您应该在调用
poWaitOnExit
之前在options中设置Execute
选项,以便Execute将阻塞,直到进程退出。否则,调用AProcess.WaitOnExit
显式等待进程退出。
关于ssh - 系统命令执行但立即后台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32366090/