Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过Python编写的脚本远程终止进程(使用SSH)时处理引用语法错误_Python_Bash_Shell_Syntax Error - Fatal编程技术网

通过Python编写的脚本远程终止进程(使用SSH)时处理引用语法错误

通过Python编写的脚本远程终止进程(使用SSH)时处理引用语法错误,python,bash,shell,syntax-error,Python,Bash,Shell,Syntax Error,我有一个python脚本文件foo.py,它有一个远程删除进程的函数 代码片段 我使用以下命令:- import os def exit_app(): os.system("ssh -tt nvidia@10.x.x.xxx "kill -9 \$(ps -aux | grep 'start_with_nn_obj_detection.bash' | awk '{print \$2}')"") exit() 我收到语法无效的错误消息。它与单引号和双引号的位置有关 错误: 我尝试

我有一个python脚本文件foo.py,它有一个远程删除进程的函数

代码片段 我使用以下命令:-

import os
def exit_app():
    os.system("ssh -tt nvidia@10.x.x.xxx "kill -9 \$(ps -aux | grep 'start_with_nn_obj_detection.bash' | awk '{print \$2}')"")
    exit()
我收到语法无效的错误消息。它与单引号和双引号的位置有关

错误: 我尝试过的事情:- 验证命令是否:- ssh-ttnvidia@10.x.x.xxxkill-9\$ps-aux | grep'start|u与| nn|u obj|u detection.bash'| awk'{print\$2}'

在终端中使用时工作正常

尝试了“…”方法,但收到错误:-

os.systemssh-ttnvidia@10.5.3.157“kill-9\$ps-aux | grep”启动| u nn|u obj|u detection.bash'| awk'{print\$2}”

意外标记“”附近出现语法错误

那我该怎么修这条线呢

os.system("ssh -tt nvidia@10.x.x.xxx "kill -9 \$(ps -aux | grep 'start_with_nn_obj_detection.bash' | awk '{print \$2}')"")
避免操作系统;改为使用子流程,并从等式中删除一个shell

import subprocess


def exit_app():
    subprocess.call(
        ['ssh',
         '-tt',
         'nvidia@10.x.x.xxx',
         "kill $(ps -aux | awk '/start_with_nn_obj_detection.bash/ {print $2}')"
        ])
    exit()
但是,如果远程主机上有pkill,您只需运行以下操作

subprocess.call(['ssh', '-tt', 'nvidia@10.x.x.x', 'pkill "start_with_n_obj_detection.bash"'])
subprocess.call(['ssh', '-tt', 'nvidia@10.x.x.x', 'pkill "start_with_n_obj_detection.bash"'])