ssh到另一个主机,运行一个涉及其他脚本的脚本,如何只杀死其他脚本?

时间:2013-11-20 08:23:40

标签: linux bash shell ssh

假设我在192.168.0.100,在〜/中有两个bash脚本:

loop.sh:

#!/bin/bash
while true
do
    loop=1
done

和,parent.sh:

#!/bin/bash
while true
do
    ./loop.sh
    sleep 1
done

现在,我切换到机器192.168.0.101,我想ssh到192.168.0.100运行parent.sh。我用命令

ssh myname@192.168.0.100 "cd ~/; ./parent.sh"

然后,我切换回机器192.168.0.100,我运行命令

killall loop.sh

我想要的是杀死loop.sh并等待1秒然后parent.sh将重启loop.sh,

但我真正得到的是parent.sh与loop.sh一起被杀死。

所以我很困惑,为什么会发生这种情况,以及如何真正实现我想要的呢?

谢谢!

1 个答案:

答案 0 :(得分:0)

Before kill the loop.sh process

   F S UID        PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD

   0 S 1047      9984  9983  0  80   0 -  1291 wait   14:45 ?        00:00:00 /bin/bash - ./parent.sh
   1 S root      9996     2  0  80   0 -     0 worker 14:46 ?        00:00:00 [kworker/1:2]    
   0 R 1047     10001  9984 99  80   0 -  1290 -      14:46 ?        00:00:02 /bin/bash - ./loop.sh

If you kill the loop.sh process that process is execute again and again due to parent.sh process execute the loop.sh process every 1 seconds. 

After loop.sh process is killed

   F S UID        PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD

   0 S 1047      9984  9983  0  80   0 -  1291 wait   14:45 ?        00:00:00 /bin/bash - ./parent.sh
   1 S root     10005     2  0  80   0 -     0 worker 14:46 ?        00:00:00 [kworker/1:2]    
   0 R 1047     10036  9984 99  80   0 -  1290 -      14:46 ?        00:00:02 /bin/bash - ./loop.sh

If you kill the parent.sh process the loop.sh process is taken care by the init process so it also execute again and again.

After parent.sh process is killed

   F S UID        PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD

   1 S root     10005     2  0  80   0 -     0 worker 14:46 ?        00:00:00 [kwo rker/1:2]
   0 R 1047     10036     1 99  80   0 -  1290 -      14:46 ?        00:01:18 /bin/bash - ./loop.sh


You better need to kill both the process to kill whole process.