Sunday, October 9, 2022

[SOLVED] How to prevent CTRL + C command (auto exiting) on PuTTY when started using batch file

Issue

I am running putty from a batch file and executing the command from the command.txt file that runs the application thereon putty. There is one separate batch to close the application that kills the process and closes the putty Window(I need this feature).

I can add /bin/bash at the end of commands in command.txt to prevent auto terminating but I don't need this feature. Because:

  1. While running a close batch it terminates the application not closing the putty window. In the close batch, I am running pkill process_name command. (But upon running close batch putty window should also be closed)
  2. On CTRL+C putty window is being terminated. But sometimes there is a need to close the running application using the CTRL + C command and need to run some other command or restart the application that is currently running in that window manually)

Is it possible?

The batch file is being created using program and it is the following:


start "" "C:\\Program Files\\PuTTY\\putty.exe" -ssh ip -l ubuntu -i "privateKey.ppk" -m "linux_scheduler/appFiles/command1.txt" -t
start "" "C:\\Program Files\\PuTTY\\putty.exe" -ssh ip -l ubuntu -i "privateKey.ppk" -m "linux_scheduler/appFiles/command2.txt" -t

Command1.txt: printMsg is the application that is running there

cd "/home/codes/" && ./"printMsg" 
/bin/bash

Solution

We can use the trap command.

I have updated command1.txt file as:

sigint() {
    echo "Killed subshell!"    
    /bin/bash
}
trap sigint 2

cd "/home/codes/" && ./printMsg

I have added /bin/bash in sigint(). So, whenever ctrl+c is entered then only the process will be terminated.



Answered By - sachinsaini
Answer Checked By - Candace Johnson (WPSolving Volunteer)