首页 > 解决方案 > python shell两行命令远程运行时抛出错误

问题描述

作为远程 SSH 命令的一部分,我想运行两个 python shell 命令;导入然后运行命令。我习惯bash,不熟悉python,不明白如何修复这个错误。

这是我的(剥离的)脚本:

#!/usr/bin/env bash
set -e

updateTasks="\"from django_celery_beat.models import PeriodicTasks\nPeriodicTasks.update_changed()\""

ssh -n -o 'StrictHostKeyChecking=no' -t -t ec2-user@<IP address> \
       "docker exec -ti \$(docker ps --format '{{.ID}}: {{.Names}}' | grep '${service}-'|\
        cut -d: -f1) /bin/bash -c '<several project-specific commands> ./manage.py shell <<< ${updateTasks}'"

运行这个让我

Traceback (most recent call last):
  File "./manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/var/www/<project specific>/lib/python3.5/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/var/www/<project specific>/lib/python3.5/site-packages/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/var/www/<project specific>/lib/python3.5/site-packages/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/var/www/<project specific>/lib/python3.5/site-packages/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/var/www/<project specific>/lib/python3.5/site-packages/django/core/management/commands/shell.py", line 101, in handle
    exec(sys.stdin.read())
  File "<string>", line 1
    from django_celery_beat.models import PeriodicTasks\nPeriodicTasks.update_changed()
                                                                                    ^

(那个 ^ 与)终端中的 对齐,努力让它在这里匹配)

要测试它不是我的命令是错误的,如果我运行 shell_plus 并且updateTasks它只是PeriodicTasks.update_changed()它的工作原理。手动 SSH 并连续执行这两个命令也可以。只是我的语法失败了。我有什么问题?

标签: pythonbash

解决方案


  • 写一行python。用 . 加入所有语句;
  • 请注意,您不能使用像if condition: for x in y:. 与 结合使用时,这样的语句将不起作用;

    使用;而不是\n


推荐阅读