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

情到浓时终转凉″ 提交于 2019-12-18 19:08:37

问题


I have written a batch file like this:

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

Then in dummy.text I have written this command:

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.

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?


回答1:


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:

avahi-daemon ... ; /bin/bash

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


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 pause command, if needed.

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

avahi-daemon ... ; read



回答2:


As suggested by Martin I tried this step:

  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

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.



来源:https://stackoverflow.com/questions/27076891/how-to-prevent-putty-shell-from-auto-exit-after-executing-command-from-batch-fil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!