0

my cmd is

putty.exe -ssh -P 6969 -l root -pw 123 -m D:\demo.txt 10.159.106.194

Content in the demo.txt is

tail -f /root/config.ini > /dev/null

I run this cmd and checkout process statuition in linux is ps -ef :

UID        PID  PPID  C STIME TTY          TIME CMD
root     68947     1  0 11:02 ?        00:00:00 bash -c   tail -f /root/config.ini > /dev/null   
root     68970 68947  0 11:02 ?        00:00:00 tail -f /root/config.ini

the tail cmd still running , what can I do to solve this problem?

Then I have a try to change my demo.txt to follow, but did not work:

#!/bin/bash
function sleepH(){
    local sshd_pid=$(ps -p $$ -o ppid | tail -n 1)
    while true
    do
        if [[ `ps -p $$ -o ppid | tail -n 1` != $sshd_pid ]];then kill -HUP $$;fi;
        sleep 2
    done
}
sleepH &

tail -f /root/config.ini >/dev/null

it will run a function in the background to judge sshd if closed, when $$ 's parent ID has changes , like sshd close ,it will run kill -HUP $$, but it did not work. when I run putty , ps -ef :

UID        PID  PPID  C STIME TTY          TIME CMD
root     70324     1  0 12:45 ?        00:00:00 sshd: root@notty
root     70327 70324  0 12:45 ?        00:00:00 bash -c #!/bin/bash function sleepH(){ ?local sshd_pid=$(ps -p $$ -o ppid | tail -n 1) ?while true ?do ??if [[ `ps -p $$ -o ppid | tail -n 1` != $sshd_pid ]];t
root     70350 70327  0 12:45 ?        00:00:00 bash -c #!/bin/bash function sleepH(){ ?local sshd_pid=$(ps -p $$ -o ppid | tail -n 1) ?while true ?do ??if [[ `ps -p $$ -o ppid | tail -n 1` != $sshd_pid ]];t
root     70351 70327  0 12:45 ?        00:00:00 tail -f /root/config.ini

when I close putty, it will become ps -ef :

UID        PID  PPID  C STIME TTY          TIME CMD
root     70350     1  0 12:45 ?        00:00:00 bash -c #!/bin/bash function sleepH(){ ?local sshd_pid=$(ps -p $$ -o ppid | tail -n 1) ?while true ?do ??if [[ `ps -p $$ -o ppid | tail -n 1` != $sshd_pid ]];t
root     70351     1  0 12:45 ?        00:00:00 tail -f /root/config.ini
root     70387 70350  0 12:45 ?        00:00:00 sleep 2

I am so confused....

I think I find the reason, the process of putty is notty , Usually every process has its own tty,when the terminal exits, all processes belonging to this tty will be killed. But since putty's ssh is not pts, the process will not exit after it exits.

2
  • What happens if you connect normally, then run kill -HUP $$? Does bash exit upon receiving the SIGHUP? Aug 8, 2019 at 12:01
  • It will close process, but I want a Automatic processing when I close putty, it will kill all process which belong to this putty
    – 陈玉杰
    Aug 8, 2019 at 12:47

2 Answers 2

1

The question is identical to Why does my remote process still run after killing an ssh session?. Also the answer is.

By simply forcing the pseudo-TTY with -t on kill of PuTTY, the command on server end will end.

PuTTY pseudo-terminal is discussed in PuTTY: how to properly emulate -t option. However, this discussion fails to recognize the -m option you're using. That makes PuTTY behave differently.

2
  • thanks~ when I just use putty without -m option , it will create a tty for putty terminal. I find other way to solve this problem ,I will introduce later
    – 陈玉杰
    Aug 9, 2019 at 7:06
  • 1
    The -m implies -T. Use -t to override that. Though -t has other side effects. Aug 9, 2019 at 11:50
-1

just add -t option can solve this problem

1
  • This is what the answer by @Jari says already (and he even posted an explanation). So why don't you accept his answer, instead of posting an inferior copy of his answer again? Aug 17, 2019 at 16:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .