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.