【问题标题】:How to run a nohup command present in a variable using ssh?如何使用 ssh 运行变量中存在的 nohup 命令?
【发布时间】:2023-04-08 01:21:01
【问题描述】:

我是 Linux 新手。我将命令存储在变量say中

a="nohup ./startWebLogic.sh &".

我需要在远程服务器上运行此命令以及其他一些命令。我已经尝试如下...

ssh user@server << EOF
few shell commands then
eval "$a"
EOF

当我运行上面的脚本时,它在登录远程服务器后什么都不做,它既不会抛出任何错误,也不会运行 nohup 命令。我的目的是使用远程服务器上的 WebLogic 启动nohup...谢谢!!

【问题讨论】:

  • 我在 ssh 之后做了一个 echo $a,惊讶地发现 '&' 不见了,我在声明 a (a="nohup ./startWebLogic.sh \&") 时逃脱了它.现在,当我运行脚本时,它给了我错误 -sh: nohup: not found

标签: shell ssh nohup


【解决方案1】:

你可以这样运行它:

ssh user@server "$a"

变量$a 将被扩展并传递给server 的shell。

如果你想使用

ssh user@server sh -s <<EOF
few shell commands then
eval "$a"
EOF

请注意sh -s 这里和那个,我删除了&lt;&lt;EOF 之间的空格。

【讨论】:

  • 感谢 Chubin,但在远程服务器上执行 $a 之前,我还需要做一些事情。所以我使用了
  • 我已经编辑了我的答案。请检查新版本。
【解决方案2】:

使用 a="nohup ./startWebLogic.sh >> nohup.out 2>&1 &"

【讨论】:

    【解决方案3】:

    我遇到了类似的问题,尝试创建一个文件例如 a.sh:

    #!/bin/bash
    nohup ./startWebLogic.sh &
    exit 0
    

    并设置 a="./a.sh"

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 2022-08-23
      • 2017-07-19
      • 2020-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多