1

I am running this python code from the command line:

# run on command line as: python firstscript.py    
import sys, subprocess 

pid = subprocess.Popen([sys.executable, 'secondscript.py']).pid
sys.exit()

Unfortunately I can't get it to exit all the way to the command line. If I hit the enter key (on OSX) it will finally exit. Is there a way to force the script to exit all the way to the command line without lingering in this weird limbo state? Also, I don't want to redirect stdout or stderr anywhere else because if I do, I lose the ability in secondscript.py to log output to a log file.

Thanks for the help.

noctufaber
  • 764
  • 2
  • 10
  • 24
  • You may find this helpful: http://stackoverflow.com/questions/4084322/killing-a-process-created-with-pythons-subprocess-popen – Philip Tzou Oct 22 '16 at 00:59
  • Any chance firstscript.py *has* exited to the command line, but secondscript.py kept writing text to the console, hiding the prompt that was printed when the first script exited? Go read your scrollback history carefully. Pressing Enter just makes the terminal print a new, fresh prompt. – Rob Kennedy Oct 22 '16 at 01:18

2 Answers2

1

The changes below worked for me:

# run on command line as: python firstscript.py    
import sys, subprocess 

process = subprocess.Popen([sys.executable, 'secondscript.py'])
output = process.communicate()[0]
noctufaber
  • 764
  • 2
  • 10
  • 24
0

You seem to be asking if there is a better way to do this. Check out check_output. I have always found it much more convenient and fool proof compared to the lower level stuff in subprocess.

kbcool
  • 695
  • 6
  • 18