-2
abc = "/a/b/pythonprogam.py --expr '((max(max_over_time(node_load15{job=\"acb\"}[2d])) by(data) / count(node_cpu_seconds_total{mode=\"DEV\" }) by(data) ) * 100)' --fields data::50"
args = abc.split()
subprocess.run(args)

I want to execute the above command in Python. I tried and it's not working, throwing an error:

Invalid parameter 'query': parse error at char 37: unterminated quoted string

2
  • 1
    You should not use split this way as it will split at every space... This is why it complains for a non terminating quoted string... [2d])) by( is splitted... Commented Jun 17, 2024 at 12:45
  • Possible duplicate of stackoverflow.com/q/70493595 Commented Jun 17, 2024 at 13:53

1 Answer 1

1

If you want to run a shell command with subprocess, you have to pass shell=True and the entire command as one string.

abc = "/a/b/pythonprogam.py --expr '((max(max_over_time(node_load15{job=\"acb\"}[2d])) by(data) / count(node_cpu_seconds_total{mode=\"DEV\" }) by(data) ) * 100)' --fields data::50"
subprocess.run(abc, shell=True)

If you want to run a program directly, without the shell and with arguments that correspond directly to Python strings, you don’t use shell syntax like quoting.

abc = [
    "/a/b/pythonprogam.py",
    "--expr", "((max(max_over_time(node_load15{job=\"acb\"}[2d])) by(data) / count(node_cpu_seconds_total{mode=\"DEV\" }) by(data) ) * 100)",
    "--fields", "data::50",
]
subprocess.run(abc)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.