简体   繁体   English

将脚本变量保存在代码中,并在重启后将其重置

[英]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. 在我的vps中,我已经运行了4个Python脚本,已经60天了,我不重新启动vps,现在我必须这样做,但是如果我重新启动vps,我的python变量和数据将被删除,因为我没有将它们存储在文件中并且它们存储在python脚本的变量中。 my OS is Ubuntu Server 16.04 LTS and i was run my python codes with nohup command until they can run in background. 我的操作系统是Ubuntu Server 16.04 LTS,我使用nohup命令运行了python代码,直到它们可以在后台运行。 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. 现在,我需要一种方法来停止我的脚本而不删除它们的变量,并在重新启动vps之后以相同的变量和数据启动它们。 Is There Any Way That I Can Do This? 有什么办法可以做到吗? In Addition, I'm sorry for writing mistakes in my question. 另外,很抱歉在我的问题中写错了。

Python doesn't provide any way of doing this. Python没有提供任何方法。

But you might be able to use CRIU , or a similar tool, to freeze and snapshot the interpreter process. 但是您可能可以使用CRIU或类似工具冻结和快照解释器过程。 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. 1但是很有可能。 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. 这与CRIU文档中的实时迁移本质上是相同的,除了您不迁移到新计算机/容器/等,而只是迁移到同一台计算机的未来。 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. *在没有做任何重要事情的Python脚本上尝试尝试(显然不包括重新启动系统,只是kill -9可执行文件)(也许增加一个计数器,将其打印出来,睡眠一秒钟,然后重复。)*尝试在脚本上执行与您正在执行的操作类似的操作*如果可以安全地同时运行一个程序的两个副本(它们不会互相to脚,写到相同的文件,或争用相同的套接字,或类似的东西),启动第二个副本并测试转储/杀死/恢复该文件*在您的真实进程之一上尝试,仍不重启*在所有四个进程上尝试*交叉用手指牺牲一只鸡,然后真正地做。


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. 然后,您只需对实时解释器进行pdb ,然后开始转储所有内容。

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. 1.如果您依赖打开的文件,管道,套接字等,CRIU会做您想做的尽可能多的事情,这比起初您期望的要多,但仍然不是您可能想要的一切……此外,如果您重新使用几乎所有RAM,可能很难将它们恢复为完全相同的状态。 And there are probably other possible issues. 而且可能还有其他可能的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM