Terminal doesn't close after shell runs the python script

Viewed 45

I have 2 python scripts sc-1.py and sc-2.py and shell file test.sh

sc-1.py

import time 

print('Script 1 started')
time.sleep(2)
print('Script 1 ended')

sc-2.py

import subprocess

print('Script 2 started')
subprocess.Popen(["sh", "test.sh"], shell=True, stdin=None, stdout=None, stderr=None, close_fds=True)
print('Script 2 ended')

test.sh

py sc-1.py

It works but there is one problem. The command terminal doesn't end after the script is ended. enter image description here

I have to use ctrl + c to end it.

enter image description here

1 Answers

You forgot to output a final newline in your Python programs, i.e. to write

print("Script 2 ended\n")

BTW, in order to make it work on my Platform (Cygwin, Python 3), I also had to do a shell=False, otherwise it would not have executed the shell script at all; but this doesn't seem to be necessary in your case.

UPDATE : The "solution" posted here applies only when I am running sc-2.py from a interactive zsh. If I use an interactive bash, I see the same effect as the OP mentions, and adding a \n does not help. However, the script is not hanging. It is terminated, but it just appears hanging because the shell does not display its prompt.

Related