我不知道如何在远程设备上通过ssh执行这个特定的组合shell脚本命令。
#!/bin/bash
cmd=""
command="restart"
case "$command" in
restart)
cmd+="pkill -f fileA.py;"
cmd+="python3 -u fileA.py >> fileA.log &"
;;
*)
echo "Unknown command"
esac
cmd=$(ssh root@foobar $cmd)
出现的错误是:
pkill -f fileA.py;python3 -u fileA.py >> fileA.log & pkill: invalid user name: fileA.py
我知道整个字符串被解释为一个命令,但这不是我想要实现的。
我很感谢你的帮助。
发布于 2018-06-10 01:27:53
你确定你粘贴了正确的代码吗?该pkill错误消息来自使用-u
或
-U
选项。请先检查一下。
其次,你缺少引号给你带来了问题。在shell替换变量之后,您将获得以下内容:
cmd=$(ssh root@foobar pkill -f fileA.py;python3 -u fileA.py >> fileA.log &)
因此,您将终止远程系统上的进程,并在本地系统上启动它。
我认为你真的需要这个:
case "$command" in
restart)
cmd="pkill -f fileA.py; nohup python3 -u fileA.py >> fileA.log & disown"
;;
*)
echo "Unknown command"
esac
cmd=$(ssh root@foobar bash -c "$cmd")
nohup
和disown
允许后台进程在外壳退出后继续运行。
https://stackoverflow.com/questions/50776688
复制相似问题