2

I have a Python script to simulate particle dynamics. The simulations are long (can take up to a month). I save my data with a given frequency. Unfortunately, I had to kill the simulations, and lost a lot of information that I sample at the very end of the simulation, when the main loop is done and the number of simulation steps exhausted.

I would like to know if there is a way in Python to save my last configuration if the work gets killed. That is, if I write in the terminal

 killall python 

or

kill (id number of the job)

can I trigger something so that my last system configuration gets saved in a file? Can the same be done if some error arises?

ma7642
  • 139
  • 2
  • 12
  • I think you can use python exit handlers for achieve you requirements, see https://docs.python.org/3/library/atexit.html – Melan Dec 20 '19 at 09:43
  • According to the link: "The functions registered via this module are not called when the program is killed by a signal not handled by Python, when a Python fatal internal error is detected, or when os._exit() is called." I actually need something that will call my functions when a bash command is run (i.e. kill). @Melan – ma7642 Dec 20 '19 at 10:08
  • Does this answer your question? [How to process SIGTERM signal gracefully?](https://stackoverflow.com/questions/18499497/how-to-process-sigterm-signal-gracefully) – stovfl Dec 20 '19 at 11:15
  • @stovfl It does not but it puts me in the right track to look for it. I had never heard of daemons, so now I know where to look. – ma7642 Dec 20 '19 at 12:44

0 Answers0