【问题标题】:Unable to execute the command on the remote Linux hosts无法在远程 Linux 主机上执行命令
【发布时间】:2020-04-22 07:14:01
【问题描述】:

我有多个主机(超过 20 个)并且需要执行命令,但它没有按预期工作。单个命令正在所有主机上运行

ps -ef |grep \`cat/z/y/x|tail -1| awk '{print$30}'\` 

脚本:

for hsts in `cat /a/b/c/hsts.txt`
  do
  ssh ${id}@${hsts} "cat /z/y/x|tail -1" | awk '{print $30}'
  done

上面的 sn-p 会给我所有主机上的进程 ID。我想知道哪个进程正在使用此进程 ID 运行,并且我正在尝试使用以下命令,但它没有成功。任何想法我错过了什么。

ssh ${id}@{hsts} ps -ef |grep "cat/z/y/x/tail -1" |awk '{print $30}'
# above command is not working when i use in the above do loop

【问题讨论】:

  • 您的循环丢失了ps -ef。您最后的命令缺少cat 之后的空格。由于所有这些错别字,很难判断你到底做错了什么。
  • 你的服务器上有pgrep吗?
  • 您是否尝试过使用ssh -t 标志
  • 这能回答你的问题吗? bash script execute commands after ssh

标签: linux


【解决方案1】:

我有多个主机(超过 20 个),需要执行命令,但它没有按预期工作。

  1. 此代码的 sn-p 在多个主机上运行,​​并为我提供了我正在运行的主机上的进程 ID。

for hsts in cat /a/b/c//hsts.txt # hsts.txt 文件包含所有主机 做 ssh ${id}@${hsts} "cat /z/y/x|tail -1" | awk '{打印 $30}' 完成

  1. 我需要修改上面的命令,我需要根据从上面的代码 sn-p 获得的进程 ID 查看正在运行的进程。

下面的 sn-p 在各个主机上运行,​​它向我显示了该过程,但如果我将上述内容合并到 for 循环中,它就不起作用。感谢您对此的帮助。 ps -ef |grep `cat /z/y/x|tail -1 |awk '{print $30}'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-06
    • 1970-01-01
    • 1970-01-01
    • 2014-04-25
    • 1970-01-01
    • 2011-12-18
    • 1970-01-01
    • 2012-07-27
    相关资源
    最近更新 更多