Search code examples
pythonpython-3.xbackupvpsubuntu-server

Save a Script Variables inside code and reset them after reboot


in my vps i have run 4 Python Script and its been 60 days that i don't reboot my vps and now i have to, but if i reboot vps my python Variables & data will be removed because i don't store them in file and they are store in variables in python script. my OS is Ubuntu Server 16.04 LTS and i was run my python codes with nohup command until they can run in background. Now i need a way to stop my scripts without removing they variables and start them with same variables and data after i reboot my vps. Is There Any Way That I Can Do This? In Addition, I'm sorry for writing mistakes in my question.


Solution

  • Python doesn't provide any way of doing this.

    But you might be able to use CRIU, or a similar tool, to freeze and snapshot the interpreter process. Then, after restart, you can resume the snapshot into a new process that just picks up exactly where you left off.

    It may not work.1 But there's a good chance it will. This is essentially the same thing as a Live Migration in the CRIU docs, except that you're not migrating to a new computer/container/etc., just to the future of the same computer. So, start reading with that page, and follow the links from there.

    You should probably test before you commit to it. * Try it (obviously don't include the system restart, just kill -9 the executable) on a Python script that doesn't do anything important (maybe increments a counter, print it out, sleep for a second, repeat. * Maybe try it on a script that does similar kinds of stuff to what yours are doing. * If it's safe to have two copies of one of your programs running at the same time (they're not going to stomp all over each other writing to the same file, or fight over the same socket, or whatever), start a second copy and test dump/kill/resume that. * Try it on one of your real processes, still without restart. * Try it on all four. * Cross your fingers, sacrifice a chicken, and do it for real.


    If that doesn't pan out, the only option I can think of is to go through your scripts, manually figure out everything that needs to be saved and how it could be accessed from the top-level global, and do that in the debugger.

    Ideally, you'll write a script that will automate accessing and saving all that stuff—plus another one to feed it into a new instance at restart. Then you just pdb the live interpreters and start dumping everything.

    This is guaranteed to be a whole lot of work, and not much fun. On the plus side, it is guaranteed to work if you do it right. On the third hand, it's pretty easy to not do it right.


    1. If you rely on open files, pipes, sockets, etc., CRIU does about as much as you could do, which is more than you might expect at first, but still not everything you could possibly want… Also, if you're using almost all of your RAM, it can be hard to wedge things back into exactly the same state. And there are probably other possible issues.