The command you have the shell run is something like this, with 1234
being a value from var
(this is passed as argument to sh -c
):
ssh $servername /usr/local/bin/pstat $1|awk 'FNR==5 {print $9}' 1234
There, $1
is expanded into the first argument of the shell, but it doesn't have have any, so that disappears. 1234
is an argument to awk
, which takes it as the name of an input file, tries to open it and fails.
I'm assuming you want the value from var
as an argument to pstat
instead. You have two options: either place the number where you have $1
now, with %s
as you did above; or use $1
, and pass var
as an argument to the shell that Popen
runs.
With %s
:
cmd=(''' ssh "$servername" /usr/local/bin/pstat '%s' | awk 'FNR==5 {print $9}' ''' % int(var)) p=subprocess.Popen(cmd, shell=True, ... )
Note that this drops the variable value in the middle of the shell command, so you do need to know it doesn't contain anything dangerous. With single-quotes around it, only single-quotes will cause issues. Since you only have a number, this should be safe.
With $1
:
cmd=(''' ssh "$servername" /usr/local/bin/pstat "$1" | awk 'FNR==5 {print $9}' ''') p=subprocess.Popen([cmd, "sh", str(var)], shell=True, ... )
Here, we pass an array as the first argument of Popen
, which makes it use the first array element as the command line, and the rest as arguments to the shell. (i.e. it runs sh
with the arguments -c
, cmdline...
, sh
and 1234
. The sh
string goes to the shell's $0
.)
This is safer than the previous in that variable value is never mixed with the code the shell runs (but note that you need the double quotes around "$1"
for the usual reasons).
Note that you need to have servername
set in the environment for that variable to be expanded by the shell Popen
runs.
cmd
string, tuple, whatever, be when you use it inPopen()
? I'm not a Python programmer.cmd
output from shell command should be job submission time in the form of HH:MM:SScmd
?