How to remain in session after exiting tailf command from remote script

0

Have PowerShell script from which I am executing the following command: putty.exe -ssh user@srv -pw password -m Execute_Command_File -t

During the script, tailf /dir/log/ command is written into Execute_Command_File. After executing the script, requested session is opened and tailf is working.

The problem, when I trying to exit from tailf(ctrl+C), it closes the terminal.

Tried to add /bin/bash at the end of Execute_Command_File, not helping. And of course tried tail -f/F, also not working...

Any ideas?

igor

Posted 2018-02-04T10:55:09.460

Reputation: 253

Answers

1

It happens that tail dies because of the CTRL+C but it is also sent (SINGINT) to the parent, bash. Since by default bash dies when receive such signal, you have to replace the default behavior of bash when receives it.

Use trap built-in command of bash(1) to change this.

Following script tailf-ctrl.sh is a demo and shows the answer:

#!/bin/bash
function finish {
    echo "CTRL-C pressed!"
}

F=testfile

echo hello > $F
# set custom action
trap finish SIGINT # comment this to see the problem
tail -f $F
# reset default action
trap - SIGINT

echo "Hello after" > after
cat after

note that:

  1. SIGINT is the signal related to CTRL+C
  2. first trap install a custom action related to the SIGINT signal
  3. second trap reset default behavoir of the SIGINT signal

Output of the script is:

$ bash tailf-ctrl.sh 
hello
^CCTRL-C pressed!
Hello after

that shows that the second file is written so the end of script is reached when tail dies because of the CTRL-C.

if you comment out the first trap command, you see your problem appears: bash immediately terminates and the output should be:

$ bash tailf-ctrl.sh 
hello
^C
$

bzimage

Posted 2018-02-04T10:55:09.460

Reputation: 36

Worked as expected, thx. Wasn't familiar with trap, seems to be very useful – igor – 2018-02-08T07:02:56.930