-8

Need to save the script state before reboot and then on resume, i need to start the script from the point it was left.

Mine is a Windows 2k12 System on a Dell R710 Server. What I have done so far is:

  1. Pickled the step number before which I need to reboot the machine. pickle.dump( self.boot_flag, open( 'c:\data.pkl', 'wb'))
  2. i give a "os.system("shutdown -r -t 0 -f")" My question now is, show to pass the step number that i have saved, so that my script resumes from there?
Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Ai_Nebula
  • 39
  • 9
  • 2
    You problem is not trivial. Unluckily you did not give any details. To give any assistance we need more information about your program, it's data model and the OS you are running it on. – Klaus D. Nov 03 '14 at 09:10
  • Mine is a Windows 2k12 System on a Dell R710 Server. What I have done so far is: 1. Pickled the step number before which I need to reboot the machine. pickle.dump( self.boot_flag, open( 'c:\data.pkl', 'wb')) 2. i give a "os.system("shutdown -r -t 0 -f")" My question now is, show to pass the step number that i have saved, so that my script resumes from there? – Ai_Nebula Nov 03 '14 at 11:06
  • If you would like to provide an answer to your question then that should be posted in the answer field below. None of the information or details about voting, edits made to this questions, or skill level at the time of posting are relevant to your core question (which had received an answer) and should not be included here. – Henry Ecker Apr 08 '23 at 14:48

1 Answers1

2

You have a series of problems :

  1. Saving state (including curent execution points),
  2. detecting shutdown/reboot,
  3. restarting after shutdown.

I would suggest that actually your are best off ignoring the middle issue, and ensure that your application keeps a minimal amount of internal state - i.e. store all of your data in an external database etc.

Edit : Make sure that you can save your state in small atomic chunks. An application I have written uses a transactional based relational database, and saves state in a set of three tables) - I also have a system which can detect errors after the fact. The advantage of a database is that it has it's own recover mechanism in case of failures.

If you think about what you need to restart, then you can ensure that when your application restarts it is almost exactly the same state. About the only thing you wont be able to do is restart execution at exactly the same line of code, but actually it may be that that is not the issue, and the issue is maintaining continuity of functionality (i.e. apart from timestamps you can't detect an interruption).

Edit : As an example my application reads the database every time it starts up, looking for records not marked as complete - in this way it can resume the functionality, at almost any point.

Restarting is pretty simple, write your code so it can run as a service.

Edit : The worst case actually isn't a restart/rebot - in these cases all the running processes are asked to shutdown nicely - and you can trap that signal and do something at least (so long as it is quick). The worst case is a power off/crash (due to overheat for instance), then you get no warning and have zero chance of a clean shutdown. In designing your application you need to consider what is the worse thing that can happen.

Tony Suffolk 66
  • 9,358
  • 3
  • 30
  • 33
  • Hi, thanks for help here. Mine is a Windows 2k12 System on a Dell R710 Server. What I have done so far is: 1. Pickled the step number before which I need to reboot the machine. pickle.dump( self.boot_flag, open( 'c:\data.pkl', 'wb')) 2. i give a "os.system("shutdown -r -t 0 -f")" My question now is, show to pass the step number that i have saved, so that my script resumes from there? – Ai_Nebula Nov 03 '14 at 11:07
  • 1
    save the step number in your pickle data. Load the pickle data after you restart, and then call that step. - or is your question more complex than that ? – Tony Suffolk 66 Nov 03 '14 at 11:14
  • Yes, i get it. Now when i load the saved step number, how does the script understand that the number being loaded is step number and not something else? Or do i need to need to do something else as well? Question 2: How do i load the script after reboot? Batch file? – Ai_Nebula Nov 04 '14 at 05:15
  • 1
    when you load the data - your program is the one that saved it, so you know what it saved, and how to interpret it ! Question 2 - load your save file when your application starts up - that way you will always restart where you left off. – Tony Suffolk 66 Nov 04 '14 at 21:12