Home > Back-end >  Running a shell script using Subprocess in Python does not produce output
Running a shell script using Subprocess in Python does not produce output

Time:11-25

I am trying to run a shell script using through Python using subprocess.Popen().

The shell script just has the following lines:

#!/bin/sh
echo Hello World

Following is the Python code:

print("RUNNNING SHELL SCRIPT NOW")
shellscript = subprocess.Popen(['km/example/example1/source/test.sh'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
shellscript.wait()

for line in shellscript.stdout.readlines():
    print(line)
print("SHELL SCRIPT RUN ENDED")

However, on running this, I am only getting the following output:

RUNNNING SHELL SCRIPT NOW
SHELL SCRIPT RUN ENDED

i.e. I am not getting the shell script output in between these 2 lines.

Moreover, when I remove the stderr=subprocess.PIPE part from the subprocess, I get the following output:

RUNNNING SHELL SCRIPT NOW
'km' is not defined as an internal or external command.
SHELL SCRIPT RUN ENDED

I am not able to understand how to resolve this, and run the shell script properly. Kindly guide. Thanks.

UPDATE:

I also tried the following change:

print("RUNNNING SHELL SCRIPT NOW")
shellscript = subprocess.Popen(['km/example/example1/source/test.sh'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

out, err = shellscript.communicate()
print(out)
print("SHELL SCRIPT RUN ENDED")

I get the following output:

RUNNNING SHELL SCRIPT NOW
b''
SHELL SCRIPT RUN ENDED

CodePudding user response:

The simple and straightforward fix is to not use bare Popen for this.

You also don't need a shell to run a subprocess; if the subprocess is a shell script, that subprocess itself will be a shell, but you don't need the help of the shell to run that script.

proc = subprocess.run(
    ['km/example/example1/source/test.sh'],
    check=True, capture_output=True, text=True)
out = proc.stdout

If you really need to use Popen, you need to understand its processing model. But if you are just trying to get the job done, the simple answer is don't use Popen.

The error message actually looks like you are on Windows, and it tries to run km via cmd which thinks the slashes are option separators, not directory separators. Removing the shell=True avoids this complication, and just starts a process with the requested name. (This of course still requires that the file exists in the relative file name you are specifying. Perhaps see also What exactly is current working directory? and also perhaps switch to native Windows backslashes, with an r'...' string to prevent Python from trying to interpret the backslashes.)

  • Related