1

I am running a remote Python script on AWS (EC2 ubuntu) in background. The script performs some file manipulations, launches a long running simulation (subprocess run with os.system(...)) and writes some log files. I would like to manage the status of the running script and hopefully exit gracefully from various conditions. Specifically:

  1. The sub-process is interrupted by the user with signal 15.
  2. The simulation (sub-process) fails (signal 8 - Floating point exception)
  3. The vm is rebooted
  4. The vm is terminated. I am using Elastic File System, so even if the instance is destroyed, all the files are not.

I know how to handle basic exceptions, but I am a bit lost when I need to catch exceptions from subprocesses. Can you recommend a solid approach?

EDIT: Please notice the bold part.

2 Answers 2

2

For your given scenarios, try with signal handling. In given cases, case 1 (signal 15) and case 3 (vm is getting rebooted), are similar(generally signal 15/SIGTERM is part of shutdown sequence or maybe triggered by user with proper privileges. Nonetheless it serves the required purpose). signal 8 - SIGFPE

import signal

def signalHandler(sigNum, frameObject):
    if sigNum == 15:
       # Code for handling signal 15 goes here
    elif sigNum == 8:
       # Code for handling signal 8 goes here

signal.signal(signal.SIGTERM, signalHandler) # signal 15
signal.signal(signal.SIGFPE, signalHandler)  # signal 8
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks this helped me to understand, but in this way I cannot handle the signals to the sub-process. If the sub-process is killed, the Python script just thinks that it is completed and it goes to the next line. My bad, I should have specified. See revised question.
Have you tried cathing Floating Point Exception? This might help.
How would that help. FPE is more of an exception (no pun).
This actually worked anyway. SIGTERM also kills the main Python script.
1

I might be misunderstanding you but just put all exception causing code in try-except blocks. You seem pretty knowledgeable but I'll give an example anyways

try:
    //some potentially error causing code
except (errorType): //need to know what type of exception it will throw
    //code for what to do if the error occurs

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.