3

I'm having a curious problem. I have a bash script that is calling a python script within it. The python script executes successfully, but never fully terminates

Content of Bash script:

#! /usr/bin/env bash
python python_script.py
echo "bar"

content of Python script:

#Much stuff
sys.exit("The python script just ended")

What I expect to see on termination would be:

>The python script just ended
>bar

What I instead get is:

>The python script just ended

If I keyboard interrupt, the bash continues as:

^C>bar

What gives? Clearly the exit is calling properly, and there is nothing between that and the output statement in the bash script that called the python script.

(I can't necessarily give specifics on the workings of the "Much stuff" in the python script, as I'm modifying existing code that I don't fully understand. I've mostly left the workings of the script alone, and modified output more than anything for formatting, but I'm happy to try and provide you with any additional information requested)

  • Can you reproduce this with a simpler Python script? – robert May 18 '12 at 13:22
  • That's weird, for me it works (only having sys.exit("The python script...") and echo bar in the bash script). – Niek de Klein May 18 '12 at 13:22
  • Does it still break if you remove everything apart from the `sys.exit()` line from the .py file? If not, the problem probably lies somewhere in `#Much stuff` – cdlk May 18 '12 at 13:28
  • It does work if everythin in `#Much stuff` is removed. I just am unsure as to what I modified in that that caused this. I diff'd the original against my modified version and the only significant changes I could see were print formatting. – TrialbyCode May 18 '12 at 13:35

2 Answers2

5

What sys.exit() does is throw an exception of type SystemExit. If your script were to catch this exception, it would continue executing past sys.exit().

Also, if you have non-daemon threads, these could be preventing the process from terminating.

If that's the case, you can either turn them into daemon threads, or somehow signal to them that you wish to exit the script, and let them shut themselves down.

Finally, there's os._exit(), but you should not have to resort to that.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Is there a way to more completely terminate the script? The code I'm using does use some threads. (thank you for your quick answer by the way) – TrialbyCode May 18 '12 at 13:32
  • @TrialbyCode: The cleanest is probably to turn them into daemon threads. I've expanded the answer. – NPE May 18 '12 at 13:51
  • I had one non-daemon thread running. Daemonizing it fixed the exit hangup, but caused some cosmetic damage to my output, but now I know where my issue is. Now I just have to figure out why that happened as I didn't modify that section of the code at all. Thank you so much for your help @aix. – TrialbyCode May 18 '12 at 13:56
0

The following also works:

import os
print('Hellow world')
os._exit(os.EX_OK)

In Ubuntu, it exits the terminal. See also the example in this link Program With the os.exit() Method