Catching the output of scontrol show job ID with python subprocess

Question:

I’m trying to return the output of the slurm command scontrol show job ID where ID is the ID number of the slurm job, to python as a string. I’m trying to use python’s subprocess command by trying

x = subprocess.check_output("scontrol show job 62988265")

However this results in the following error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.6/subprocess.py", line 356, in check_output
    **kwargs).stdout
  File "/usr/lib64/python3.6/subprocess.py", line 423, in run
    with Popen(*popenargs, **kwargs) as process:
  File "/usr/lib64/python3.6/subprocess.py", line 729, in __init__
    restore_signals, start_new_session)
  File "/usr/lib64/python3.6/subprocess.py", line 1364, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'scontrol show job 62988265': 'scontrol show job 62988265'

What am I missing? I’m confused as to what the command and argument are in this case.

Asked By: sodiumnitrate

||

Answers:

The check_output method expects a list of parameters, not a string ; it will not parse and tokenise the string hence looking for a single executable named scontrol show job 33087539.

$ python
Python 3.8.6 (default, Apr  5 2021, 11:09:18)
[GCC 10.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> x = subprocess.check_output("scontrol show job 33087539")
Traceback (most recent call last):
[...]
FileNotFoundError: [Errno 2] No such file or directory: 'scontrol show job 33087539'

Use the split method like this:

>>> x = subprocess.check_output("scontrol show job 33087539".split())
>>> print(x)
b'JobId= 33087539 JobName=bashn
[...]
Answered By: damienfrancois
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.