在后台无限循环中执行命令的脚本

<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中的一些帖子,但无法找到此问题的确切解决方案。

最佳答案

使用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

这遵循您当前将远程命令放入变量中的做法,但是应该考虑其他地方引用的与之相反的参数。

09-04 18:04
查看更多