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.

陈玉杰
  • 1
  • 1
  • 3

2 Answers2

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.

-1

just add -t option can solve this problem

陈玉杰
  • 1
  • 1
  • 3