python - 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 并连续执行这两个命令也可以。只是我的语法失败了。我有什么问题?
解决方案
- 写一行python。用 . 加入所有语句
;
。 - 请注意,您不能使用像
if condition:
for x in y:
. 与 结合使用时,这样的语句将不起作用;
。使用
;
而不是\n
推荐阅读
- sql-server - sql查询根据子字符串过滤结果
- python - 在 python 或 bash(或两者兼有!)中为终端会话的范围设置环境变量
- python - 在 Python 3 中创建一个存储字典并在字典值更改时设置对象属性的类
- bootstrap-4 - 另一个非引导网站上的独立 Bootstrap 4 导航栏
- r - 预加载的闪亮服务器应用程序工作正常,但自己的应用程序无法启动?
- mnist - MNIST 和 SVHN 调整大小
- jquery - 如何使用 jquery each() 和 hasClass() 匹配所有匹配的类
- c++ - 如何使用专用线程类 MyThreadClass
- javascript - 内联和不同行编写 React 组件打开和关闭有什么区别?
- c - 内存泄漏。无法释放 malloc'ed char*