[BangPypers] Restart when python script hangs
sateeshpyper at gmail.com
Mon Jun 2 15:26:33 CEST 2014
On Mon, Jun 2, 2014 at 3:14 PM, Rahul Gopan <rahulpcet19 at gmail.com> wrote:
> When I run a python script it hangs at random places. Is there any way to
> identify when the script hangs and to resume it automatically. When I see
> that it is not responding i do ^Z (Ctrl -Z) to stop and then use fg command
> to start it again. I want to automate this.
As others have already suggested it is not a good idea to silently ignore
so you should debug your script to understand where exactly the script
You can get a trace of the script state when you press 'Ctrl+Z' by handling
the signal and making use of the 'inspect' module.
Consider the below sample code:
% cat signal.py
# Register function 'signal_handler' to handle the signal 'Ctrl+Z'
def signal_handler(signal, frame):
print "You pressed Ctrl+Z\n";
if __name__ == '__main__':
When the above script is running, pressing 'Ctrl+Z' would yield below
the script would continue its execution.
% python signal.py
You pressed Ctrl+Z
(<frame object at 0x1a7ae770>, 'signal.py', 16, 'signal_check', ['
The output from inspect:
will give details like name of the function which resulted in call for
signal handler, line number etc.
Refer to the documentation of 'signal' module for more details on using
this module for signal handling.
More information about the BangPypers