Linux ssh到另一个主机,运行一个涉及另一个脚本的脚本,如何只杀死另一个脚本?
假设我在192.168.0.100中,在~/中有两个bash脚本: loop.sh:Linux ssh到另一个主机,运行一个涉及另一个脚本的脚本,如何只杀死另一个脚本?,linux,bash,shell,ssh,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
#!/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"
killall loop.sh
然后,我切换回机器192.168.0.100,运行命令
ssh myname@192.168.0.100 "cd ~/; ./parent.sh"
killall loop.sh
我想要的是杀死loop.sh并等待1秒,然后parent.sh将重新启动loop.sh
但我真正得到的是parent.sh和loop.sh一起被杀
所以我很困惑,为什么会发生这种情况,以及如何真正实现我想要的
谢谢 很抱歉,我尝试了您的设置,它按照您的预期工作:loop.sh在杀死它后一秒钟重新启动。我在我的环境中运行了它,但它按照预期工作,但它仍然没有按照我的预期工作,我不知道重新启动计算机是否可以修复此问题。
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.