1

I'm trying to execute a python script using php with an exec command like this:

exec("python /address/to/script.py");

I don't need the script to run to completion, so after it does what I need, I call sys.exit() from within it. Execution is passed back to the php script, which is great, however the python process is still running. I can see it in my server's process list. Is there more that's required to fully kill it?

Additional Info

  • The python script was written by a third party.

  • I know very little about python, just enough to add the sys.exit() call.

DaiBu
  • 529
  • 3
  • 17

1 Answers1

0

The script could still be executing some cleanup code, or you could be calling sys.exit() from a child process which will essentially be calling thread.exit(), leaving the parent process running.

Check that the sys.exit() call is in the main part of the script and that no error handling is interfering with the SystemExit exception, or alternatively you could try os._exit(). Also ensure that an ampersand (&) is not present within the command passed to exec() as this will cause the script to run as a background process.

Note that os._exit() is not favourable since it doesn't do any cleanup, and essentially ends the process immediately.

Edit To end the script from within your try block you could do something like this:

try:
    # Existing Code
except SysExit:
    os._exit() # quit the process
except:
    # Existing error handling

Ideally the application logic should make use of message passing or something similar so that a child thread could notify the main thread that it should terminate.

Daniel Waghorn
  • 2,997
  • 2
  • 20
  • 33
  • No, & is not present. I need to execute it syncronously. Is there a way to determine within the script if I'm inside a child process? I know how to print output. Anyways, os._exit() sounds like it should be fine. As far as cleanup, do you just mean program specific stuff? The server state should be unaffected if I repeatedly killed the script forcefully every time, right? – DaiBu Jul 24 '16 at 18:36
  • There's some techniques for checking whether child processes are running [here](http://stackoverflow.com/questions/23442651/check-if-the-main-thread-is-still-alive-from-another-thread) that you could try. Cleanup would be program specific stuff, flushing buffers etc. although it shouldn't affect the server state. It's no different than killing the process via the `kill` command in the terminal. – Daniel Waghorn Jul 24 '16 at 18:50
  • Well I tried using `os._exit()` and it seems to trigger exception handling in their script. Still doesn't die, just outputs a message to the terminal. But I found `search_thread = Thread()` and `search_thread.start()` calls in the main file (the file I'm `sys.exit()`-ing from runs a search loop, so it's probably the child thread). Is there a simple way to send a notification to the parent and kill it there? – DaiBu Jul 24 '16 at 18:52
  • Ah, yup. The search loop is within a `try:` block. Is there a way to disable exception handling only for kill commands? – DaiBu Jul 24 '16 at 18:54
  • What to do is add another `except:` clause above all of the others to catch the `SystemExit` and then call `sys.exit()`. See my edit above. – Daniel Waghorn Jul 24 '16 at 18:57
  • If that doesn't work you'll need to use flags between the main process and threads. There's an example [here](http://stackoverflow.com/questions/18064901/kill-main-thead-from-child-thread-python). – Daniel Waghorn Jul 24 '16 at 19:00
  • Got it. I had to use `os._exit()` in the exception handler instead of `sys.exit()` though. I think that's what you meant. Might wanna update the answer in case someone else stumbles here. Thanks brotha. – DaiBu Jul 24 '16 at 19:41
  • Nice one! Thanks will update it for future reference. – Daniel Waghorn Jul 24 '16 at 21:50