1

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.

user1164061
  • 4,222
  • 13
  • 48
  • 74

2 Answers2

2

I'm not sure about jython specifically, but the subprocess documentation suggests that your command needs to be a list, not a string, unless you set the shell variable to True. Your code should work if you change your call to be subprocess.call(mycmd, shell=True), but please be sure to read the warnings in the documentation about the security risks inherent in setting shell to True.

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 using subprocess.Popen.

MPlanchard
  • 1,798
  • 1
  • 16
  • 13
2

As already stated, subprocess can't handle single str inputs and shell metacharacters like | unless shell=True. But in this case, you really don't need a pipe anyway. You can have Python do the filtering and avoid the pipe to findstr completely:

# sc query command only, as list which gets better safety/performance
mycmd = ["sc", "query", service_name]

# Open command to run asynchronously, capturing output
proc = subprocess.Popen(mycmd, stdout=subprocess.PIPE)

# Wait for process to complete while slurping output
stdout, _ = proc.communicate()

# Check if expected output was seen and process exited successfully
if state in stdout and proc.returncode == 0:
    print("Service stopped successfully")
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • Hi! What is "state" in the code? Am getting the following error: if state in stdout and proc.returncode == 0: NameError: name 'state' is not defined – user1164061 Mar 01 '16 at 21:51
  • Sorry.. I understaood what the state is. I added an else to "if state in stdout and proc.returncode==0" to print error. When I execute it, it is always going to the else part . I think it is not evaluating state in stdout. – user1164061 Mar 01 '16 at 22:02
  • I printed stdout and found that state is "STOP PENDING". Thats why it is failing. I will write a while do loop to keep querying until this changes to STOPPED. That should help – user1164061 Mar 01 '16 at 22:06
  • @user1164061: Glad you figured it out. :-) Wasn't at a computer to see your comments until just now. – ShadowRanger Mar 01 '16 at 23:30
  • Thank you very much for helping me. – user1164061 Mar 02 '16 at 15:52