0

I have a shelve-based Python 3.6 application running on a Linux system with basically this code:

try:
    run()
finally:
    write_to_db()

When I was developing and stopped it with KeyboardInterrupt that was fine. Then I decided to run it in the background from a shell script:

#!/bin/bash

nohup ./application &

I also decided to let others test it out.

After a while I realized I needed to add a signal handler, so I have a way to terminate it properly and handle a server reboot. I've done that, and I've tested it.

The only problem is: Even though it was cleary stated that it was only for testing at his stage, people have (of course) spent a lot of time and effort adding real data in the version without a signal handler.

So the question is: Can anyone think of a way to salvage that data from memory? I thought sending a SIGINT would create a KeyboardInterrupt, but apparently not. Is there any other way to introduce an exception?

  • something like this? https://stackoverflow.com/questions/18499497/how-to-process-sigterm-signal-gracefully – Jean-François Fabre Sep 14 '18 at 08:28
  • No. (See my comment to your answer.) – Magnus Gustavsson Sep 14 '18 at 08:59
  • you could set a variable in your handler, and check it in your processing to throw an exception if variable is set. Or set some very heavily used global variable to "None" so it crashes when accessing it (dirty...) – Jean-François Fabre Sep 14 '18 at 09:29
  • 1
    I cannot reproduce what you're describing: `kill -s SIGINT ` to a python process started using `nohup python script.py &` does trigger a `KeyboardInterrupt` – Nils Werner Sep 14 '18 at 09:37
  • @Jean-FrançoisFabre I've already fixed the code by adding a `signal` handler. The problem is salvaging data from unfixed _running_ code. – Magnus Gustavsson Sep 14 '18 at 11:07
  • @NilsWerner Hmm. Thanks. According to the docs (https://docs.python.org/3.6/library/signal.html) it should indeed. It doesn't though, but it's something to look into. Might have something to do with that fact that I'm using coroutines? Triggering `KeyboardInterrupt` using Ctrl-C worked fine with the same code though. – Magnus Gustavsson Sep 14 '18 at 11:10
  • Anything useful in `nohup.out`? – Nils Werner Sep 14 '18 at 11:13
  • @NilsWerner No. However, I did put `nohup script.py &` in a shell script that I ran. I've tested doing the same thing with a very simple python program, and it doesn't respond to `SIGINT` either. – Magnus Gustavsson Sep 14 '18 at 12:17
  • Edited the question to add that information. – Magnus Gustavsson Sep 14 '18 at 12:28
  • Can you amend your post with the actual code that reproduces this? – Nils Werner Sep 14 '18 at 12:49
  • Sure. I've included the actual shell script in it's entirety. I'm not adding the actual code for `run()` and `write_to_db()`, but `while True: pass` and `pass` seems to work well enough for testing purposes. Still can't reproduce? – Magnus Gustavsson Sep 14 '18 at 13:39
  • You mean you want actual `shelve` code to test if it gets written property? Suppose I could create something like that. – Magnus Gustavsson Sep 14 '18 at 13:53
  • @NilsWerner I'm sorry if that wasn't the answer you wanted. If so, please help me out a little more? I want to write a good post, but I can't see how I can improve it at this point. It's a fairly large application, and surely you can't want me to post the _complete_ code? I do believe I have included everything I can that's relevant to the topic. Sure, I suppose I can post the actual top level code from the main file instead of `run()` and `write_to_db()`, but it creates objects and calls methods in them and I don't see how it would help anyone in any way. – Magnus Gustavsson Sep 14 '18 at 15:22
  • I am saying: The code you posted will terminate if you send it `kill -s SIGINT`, so it does not reproduce the problem. Write the smallest example that reproduces the issue you are reporting. – Nils Werner Sep 14 '18 at 20:15

0 Answers0