Alex
Alex

Reputation: 2805

python shell two-line command throws error when run remotely

As part of a remote SSH command I want to run two python shell commands; to import and then run a command. I'm used to bash, am not familiar with python, and don't understand how to fix this error.

Here's my (stripped) script:

#!/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}'"

Running this gets me

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()
                                                                                    ^

(that ^ lines up with the ) in the terminal, struggling to get it to match here)

To test it's not my commands that're wrong, if I run shell_plus instead and updateTasks is just PeriodicTasks.update_changed() it works. SSHing in manually and executing the two commands in succession also works. It's just something about my syntax that's failing. What do I have wrong?

Upvotes: 0

Views: 47

Answers (1)

Poojan
Poojan

Reputation: 3519

  • To write one line python. Join all statement with ;.
  • Note that you can not use block statements like if condition: for x in y:. Statements like this will not work when combining with ;.

    use ; instead of \n

Upvotes: 1

Related Questions