ssh - 系统命令执行但立即后台

标签 ssh pascal lazarus freepascal delphi

我正在尝试使用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;
TempFileTProfile类型的变量,它是包含有关服务器信息的记录。编目系统和检索工作正常,但拉起 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/

相关文章:

linux - 使用 Net::Telnet ssh 进入 linux

linux - 逐行专门调试

delphi - 将 TStack 代码从 Delphi 转换为 Lazarus

c++ - 如何使用 “PASCAL Annotation”文件使用C++训练SVM

delphi - TOpenDialog 和空间

linux - Indy 10 以 1024 个 block 发送数据。如何增加 block 大小?

git - 我在git仓库中看不到文件

mercurial - 每次使用 Mercurial 时,ssh 都不会要求输入密码

java - 无法使用 jcraft JSch 连接到 SFTP

delphi - 是否可以将语句转换为表达式?