3

This question extends/revives this one. The relevance to revive this topic is due to the failure in solving the same problem with the given answers.

The bash script executes a python script embedded. Something like

  #!/bin/bash
  ./pyscript.py 

chmod +x pyscript.py permission was given.

Alternative ways to run the script were used. (python -u pyscript.py or /usr/bin/python pyscript.py)

As the title states the python program does not exit.

I have tried the following attempts within the python script to solve the issue:

  1. sys.exit(0); %the program catches the correct exception
  2. os._exit(1) %does not work and the correct exception is catched
  3. sys.stdout.flush() %to clean the buffer of the stdout

The daemon solution is not suitable for what I need, because running in the background independently from the main script will not wait for the execution of the python program untill the end.

What are the alternative solutions that remain for this case?

7
  • Where do you call flush? Commented Sep 11, 2014 at 16:28
  • I've never encountered a problem like this. What does the Python script do? Commented Sep 11, 2014 at 16:29
  • What are those bits about catching the exception talking about? Commented Sep 11, 2014 at 16:44
  • The program reaches the end of the python script which has been proven by debugging. Commented Sep 11, 2014 at 18:17
  • the python script is the main entry for an entire project, where its placed the if __name__=="__main__". Commented Sep 11, 2014 at 18:22

4 Answers 4

1

Have you tried to use strace -p $PID on the python process? The output will not always be useful however.

From the code perspective, in addition to threads I would check if there are any signal handlers (which maybe do not terminate for some reason).

As far as threads are concerned, you might be interested in this, although I believe someone mentioned it in the other thread.

Sign up to request clarification or add additional context in comments.

7 Comments

Hi Michael. I am not familiar with strace. For the first try I am using the command like this strace -f -o /tmp/woo python pyscript.py. Opening the "/temp/woo", there lies the output of the system callS. The problem is that I can't figure out how to interpret the output. How does strace apply in order to solve the problem?
In addition with strace the program does not run till the end.
Hi Jack. The two tools strace and ltrace are installed in many systems and give the user a clue about system calls (strace) or library calls (ltrace) executed by the given process. You can also attach them to a running process with the -p switch. Unfortunately, their output is not always helpful. As it is a 'cheap' method (compared to using a debugger, for example), I always try it in unclear situations. In your case, I would suggest attaching strace to the python process after the program should have ended and looking at the output produced at that time.
I wouldn't expect too much, but maybe you see if the program is waiting or working, and maybe it gives you a hint what to look for next in the source code. How many LOC does your software have, if I may ask?
how can I attach the the strace to the pid if the program do not exit from the python script. To do that it seems that is necessary to run the program in background and then use the strace with the pid.
|
1

Finnally the problem is solved.

The program in python wich I've been trying to kill the process runs with multiple threads. sys.exit(0) only terminates the thread in which the program is called.

The os._exit(1) was called with the sys.exit(0) before its execution (fail!).

By running os._exit(1) without sys.exit(0) before, the program exit the python script.

The reason must be that sys.exit() only terminates the thread in which it is called and allows the program to clean resources, while os._exit() does an abrupt program termination. Found here.

With this solution it's better guarantee the termination of any task the program should end and then call os._exit.

Comments

0

what I usually do to separate a script from the main shell terminal process is sending the script inside a screen session detached. Then I kill the pid of the script without any trouble. But for this particular case I want the program waiting for the end of the python subscript and not as a parallel process.

Comments

0

Also, you might want to try the trace module, i.e. running your program with #!/usr/bin/env python -m trace --trace. If python is executing some of your code (which it probably is), it should show you details on that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.