Python subprocess call is supposed to run as command as is, but it is complaining if there is a pipe in it. Here is my code:
#!/usr/bin/python
import sys
import subprocess
import time
service_name= "mysrvc"
state ="STOPPED"
mycmd ="sc query " + service_name + " " + "|" + " findstr" + " " + state
print(mycmd)
if subprocess.call(mycmd)==0:
print("Service stopped successfully")
The Error I get is :
ERROR: Invalid Option; Would you like to see help for the QUERY and QUERYEX commands? [ y | n ]:
If I change the command to just
mycmd = "sc query " + service_name
I am able to run the script successfully. It is just the pipe and the arguments following it which is a problem. If I run sc query mysvrc | findstr STOPPED
directly on command line it works fine.
How can I get this to work? Note that I run this python script using jython2.7. I wasn't successful in using win32serviceutil because it couldn't find the module win32serviceutil.
As already stated,
subprocess
can't handle singlestr
inputs and shell metacharacters like|
unlessshell=True
. But in this case, you really don't need a pipe anyway. You can have Python do the filtering and avoid the pipe tofindstr
completely:I'm not sure about
jython
specifically, but thesubprocess
documentation suggests that your command needs to be a list, not a string, unless you set theshell
variable toTrue
. Your code should work if you change your call to besubprocess.call(mycmd, shell=True)
, but please be sure to read the warnings in the documentation about the security risks inherent in settingshell
toTrue
.If you don't want to set
shell=True
, you won't be able to use the pipe directly in your command, but there is a section in the documentation, replacing shell pipeline on how to mimic the functionality of the pipe usingsubprocess.Popen
.