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?

Community
  • 1
  • 1
Jack
  • 106
  • 1
  • 9

4 Answers4

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.

Community
  • 1
  • 1
Michael Jaros
  • 4,586
  • 1
  • 22
  • 39
  • 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? – Jack Sep 11 '14 at 21:20
  • In addition with strace the program does not run till the end. – Jack Sep 11 '14 at 21:24
  • 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. – Michael Jaros Sep 11 '14 at 21:33
  • 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? – Michael Jaros Sep 11 '14 at 21:36
  • 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. – Jack Sep 11 '14 at 21:53
  • you mean because it is blocking your terminal? You can either simply use a second terminal or use `Ctrl+Z` and then type `bg` to suspend the process and put it in the background. either way, you have to find out python's PID (let's say it is 1234) with `ps`, and then type `strace -p 1234`. when you are done, type `fg` to make python a foreground job again or kill it with `kill %1`. – Michael Jaros Sep 11 '14 at 22:36
  • the link provided in this post is helpful in answering the question. – Jack Sep 12 '14 at 12:29
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.

Jack
  • 106
  • 1
  • 9
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.

Jack
  • 106
  • 1
  • 9
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.

Michael Jaros
  • 4,586
  • 1
  • 22
  • 39