Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 999
mkkhedawat Avatar asked Nov 22 '14 11:11

mkkhedawat


People also ask

How do I stop command prompt from closing after batch file?

If you want the command prompt cmd widnow to stay open after executing the last command in batch file –you should write cmd /k command at the end of your batch file. This command will prevent the command prompt window from closing and you'll get the prompt back for giving more commands in the cmd window.

How do I keep my PuTTY window open?

To ensure that the ssh session windows does not close and to view the error message on the server side, set 'Close Window on exit' to 'Never' under the Session tab as shown below. This is useful in troubleshooting ssh failure during ssh access to the remote ssh gateway.

How do I stop scripts from closing?

One of the ways with which the user can stop the command prompt from closing is to add a word called Pause at the end of the script file. This makes the command prompt pause after the execution of the batch file till the user presses any key and closes as soon as a key is pressed.

How do I keep the command prompt window open?

Type the "cmd /k" parameter before every command to keep the window from closing.


2 Answers

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
like image 149
Martin Prikryl Avatar answered Sep 17 '22 17:09

Martin Prikryl


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.

like image 41
Sandy Avatar answered Sep 21 '22 17:09

Sandy