重新启动自我更新的python脚本 [英] Restarting a self-updating python script
问题描述
我编写了一个脚本,该脚本可以通过从网站上下载最新版本并覆盖正在运行的脚本来保持最新状态.
I have written a script that will keep itself up to date by downloading the latest version from a website and overwriting the running script.
我不确定更新脚本后重新启动脚本的最佳方法.
I am not sure what the best way to restart the script after it has been updated.
有什么想法吗?
我真的不想拥有一个单独的更新脚本. 哦,它也必须在两个linux/windows上都可以工作.
I don't really want to have a separate update script. oh and it has to work on both linux/windows too.
推荐答案
在Linux或任何其他形式的unix中, sys.executable 具有与上次执行相同的参数(sys.argv
,或多或少)或其任何变体(如果您需要通知您的下一个化身实际上是重启).在Windows上, os.spawnl (和朋友)是您可以做的最好的事情(尽管过渡期间比os.execl和朋友们要花费更多的时间和内存).
In Linux, or any other form of unix, os.execl and friends are a good choice for this -- you just need to re-exec sys.executable with the same parameters it was executed with last time (sys.argv
, more or less) or any variant thereof if you need to inform your next incarnation that it's actually a restart. On Windows, os.spawnl (and friends) is about the best you can do (though it will transiently take more time and memory than os.execl and friends would during the transition).
这篇关于重新启动自我更新的python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!