【问题标题】:ssh to another host, run an script involving the other script, how to only kill the other script?ssh 到另一个主机,运行一个涉及另一个脚本的脚本,如何只杀死另一个脚本?
【发布时间】:2013-11-20 18:35:54
【问题描述】:

假设我在 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 一起被杀死。

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

谢谢!

【问题讨论】:

  • 很抱歉,我尝试了您的设置,它按您的预期工作:loop.sh 在杀死它一秒钟后重新启动。
  • 我在我的环境中运行它,但它也按预期工作
  • 感谢您的尝试,但它仍然无法按我的预期工作,我不知道重启机器是否可以解决此问题。

标签: linux bash shell ssh


【解决方案1】:
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.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-30
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    • 2012-01-09
    • 2019-06-22
    • 2013-06-17
    • 2022-01-23
    相关资源
    最近更新 更多