簡體   English   中英

退出bash腳本時終止在遠程服務器中啟動的無限循環

[英]terminate infinite loop initiated in remote server when exiting bash script

在后台執行無限循環命令的腳本

<SOMETHING ELSE AT START OF SCRIPT>

cmd='while true;
do
    ps aux | head;
    sleep 1;
done > $FILE'    

ssh root@$SERVER $cmd &
...
...
<SOME OTHER TASKS>
...
...
( at the end of this script, how to kill the above snippet executing in remote server)
[ kindly note i dont want to wait as the while loop is infinite ]

閱讀並嘗試了stackoverflow的一些帖子,但找不到這個問題的確切解決方案。

如果要在腳本結束時終止在后台運行的ssh進程,只需執行以下操作:

kill $!

我假設這是你在后台開始的唯一(或最后一個)過程。

使用sentinel文件而不是無限循環:

cmd='while [ -r /tmp/somefile];
do
  # stuff
done > $FILE'

ssh root@$SERVER touch /tmp/somefile
ssh root@$SERVER $cmd &
# do other stuff
ssh root@$SERVER rm -f /tmp/somefile

這遵循您當前將遠程命令放在變量中的做法,但應考慮針對其他地方引用的參數。

嘗試以下順序

CTRL+Z
fg
CTRL+C

要么

jobs
kill %jobspec

要殺死屬於登錄用戶的所有內容,您可以嘗試:

 whois=`w|grep $user|awk '{print $2}'`;user=root; ssh $user@server -C "ps auwx|grep $whois|awk '{print \$2}'"

這將列出您剛剛登錄的用戶擁有的所有進程 - 只需添加| xargs kill -9

 whois=`w|grep $user|awk '{print $2}'`;user=root; ssh $user@server -C "ps auwx|grep $whois|awk '{print \$2}'|xargs kill -9  "


 whois=`w|grep $user|awk '{print $2}'`;user=root; ssh $user@server -C "ps auwx|grep $whois|awk '{print \$2}'|awk '{print "kill -9 " $1}'|/bin/sh "

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM