在 Windows 中从批处理文件执行命令后,如何防止 PuTTY shell 自动退出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27076891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:50:28  来源:igfitidea点击:

How to prevent PuTTY shell from auto-exit after executing command from batch file in Windows?

windowsshellbatch-filecmdputty

提问by mkkhedawat

I have written a batch file like this:

我写了一个这样的批处理文件:

Start putty.exe -ssh 172.17.0.52 -l root -m dummy.txt

Then in dummy.textI have written this command:

然后在dummy.text我写了这个命令:

avahi-daemon --no-drop-root -D
export XVHMI_USERCONFIG_PATH=/home/UserProfileConfig
export XDG_RUNTIME_DIR=/tmp
cd /opt/bosch/airis/bin

When I run the .bat file, PuTTY starts, commands execute (hopefully, not sure) and it exits.

当我运行 .bat 文件时,PuTTY 启动,命令执行(希望如此,不确定)并退出。

How to keep that window open?

如何保持那个窗口打开?

I have googled for the same, but no solid help. I read on stack overflow itself that we need to define something in txt file, but what and most importantly how?

我已经用谷歌搜索了相同的内容,但没有可靠的帮助。我读到堆栈溢出本身,我们需要在 txt 文件中定义一些东西,但最重要的是如何定义?

回答by Martin Prikryl

The SSH session closes (and PuTTY with it) as soon as the command finishes. Normally the "command" is shell. As you have overridden this default "command" and yet you want to run the shell nevertheless, you have to explicitly execute the shell yourself:

命令完成后,SSH 会话将关闭(并关闭 PuTTY)。通常“命令”是shell。由于您已覆盖此默认“命令”,但仍想运行 shell,因此您必须自己显式执行 shell:

avahi-daemon ... ; /bin/bash

Also as use of -mswitch implies a non-interactive terminal, you probably want to force an interactive terminal back using -tswitch.

此外,由于使用-mswitch 意味着非交互式终端,您可能希望使用-tswitch强制返回交互式终端。



Though, I'm not really sure if you want to execute shell or if you just want to see your command output. If the latter, did you consider using plink? It's console terminal client from PuTTY package. Being console application, it inherits console of parent batch file, and you can pause the batch console from closing using pausecommand, if needed.

不过,我不确定您是想执行 shell 还是只想查看命令输出。如果是后者,您是否考虑使用plink? 它是 PuTTY 包中的控制台终端客户端。作为控制台应用程序,它继承了父批处理文件的控制台pause,如果需要,您可以使用命令暂停批处理控制台的关闭。

Another option (both for PuTTY and plink) is to pause on remote side. E.g. Using readcommand.

另一种选择(对于 PuTTY 和 plink)是在远程端暂停。例如使用read命令。

avahi-daemon ... ; read

回答by Sandy

As suggested by Martin I tried this step:

按照 Martin 的建议,我尝试了这一步:

  1. putty.exe -ssh 172.17.0.52 -l root -m dummy.txt -t

  2. added /bin/bash at the end of commands in dummy.txt

  1. putty.exe -ssh 172.17.0.52 -l root -m dummy.txt -t

  2. 在 dummy.txt 的命令末尾添加 /bin/bash

It worked for me. Please note, you have to follow both the steps as mentioned above. This way you can keep the session alive and can manually execute further commands.

它对我有用。请注意,您必须按照上述两个步骤进行操作。通过这种方式,您可以保持会话处于活动状态并可以手动执行更多命令。