8

I have created a program that creates a web architecture in a local server then loads the necessary browser to display the html and php pages on localhost.

The os.system call kills the python process but doesn't kill the other processes -- for example, httpd.exe and mysqld.exe

The subprocess call kills the httpd.exe and mysqld.exe programs but continues to run the python code, and no code executes after the subprocess call.

How would i go about killing or hiding all necessary processes after the python code is executed?

Here is my code.

os.makedirs(dr + x + '/admin' + '/css')
dobj = open(dr + x + '/admin' + '/css' + '/style.css', 'w')
dobj.close()
del dobj
os.makedirs(dr + x + '/admin' + '/js')
os.makedirs(dr + x + '/admin' + '/img')
################################################################################
## THE OS SYSTEM CALLS CLOSE THE APP BUT OPEN THE PROCESSES
## AND THE SUBPROCESS CALLS CLOSE BOTH PROCESSES AND LEAVES THE APP OPEN
## CANT WIN!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
os.makedirs(dr + x + '/admin' + '/conf')
#os.system(r'C:\\xampp\\apache\\bin\\httpd.exe')
#os.system(r'C:\\xampp\\mysql\\bin\\mysqld.exe')
subprocess.Popen(['C:\\xampp\\apache\\bin\\httpd.exe'], shell=True, creationflags=subprocess.SW_HIDE)
subprocess.Popen(['C:\\xampp\\mysql\\bin\\mysqld.exe'], shell=True, creationflags=subprocess.SW_HIDE)
webbrowser.open('localhost/' + x)
sys.exit()
################################################################################


else:
    backmaybe = raw_input('Already Exists... Try Again? (Y/N) ')
if backmaybe == 'y':
    start()
else:
    sys.exit()
rypel
  • 4,686
  • 2
  • 25
  • 36
  • 1
    what is the desired running status of httpd, mysqld after `sys.exit()`? – jfs Nov 11 '12 at 19:40
  • 5
    if you want httpd, mysqld to keep running after your script exited; you could use [DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP](http://stackoverflow.com/a/13256908/4279) flag. btw, you don't need `shell=True`. – jfs Nov 11 '12 at 19:45
  • the running status of httpd and mysqld should run stealthly, ive made this very user friendly, dont need any excess windows. –  Nov 12 '12 at 09:24
  • @J.F.Sebastian How would i implement this? i am still in the early stages, but thankyou very much! –  Nov 12 '12 at 09:25
  • please, do [a minimal research yourself](http://stackoverflow.com/search?q=[python]+[windows]+subprocess+hide+console&submit=search), see [ask]. – jfs Nov 12 '12 at 09:40
  • Possible duplicate of [Calling an external command in Python](https://stackoverflow.com/questions/89228/calling-an-external-command-in-python) – Nabin Nov 22 '17 at 14:01

1 Answers1

5

The difference between os.system and subprocess.Popen is that Popen actually opens a pipe, and os.system starts a subshell, much like subprocess.call. Windows only half-supports some pipe/shell features of what *nix operating systems will, but the difference should still fundamentally be the same. A subshell doesn't let you communicate with the standard input and output of another process like a pipe does.

What you probably want is to use subprocess like you are, but then call the kill() method (from the docs) on the pipe object before your application terminates. That will let you decide when you want the process terminated. You might need to satisfy whatever i/o the process wants to do by calling pipe.communicate() and closing the pipe's file handles.

Ken Kinder
  • 12,654
  • 6
  • 50
  • 70