Python subprocess call cannot take grep

2020-04-28 07:59发布

问题:

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.

回答1:

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")


回答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.



标签: python jython