如果使用python卡滞,请尝试清除重新启动并超时
我想用python重新启动远程linux(Redhat)机器,但有时远程pc在重新启动时会卡住。如果重启时间太长,我是否可以超时?i、 如果重启时间超过100秒,是否应中止重启如果使用python卡滞,请尝试清除重新启动并超时,python,Python,我想用python重新启动远程linux(Redhat)机器,但有时远程pc在重新启动时会卡住。如果重启时间太长,我是否可以超时?i、 如果重启时间超过100秒,是否应中止重启 import os restart = raw_input("Do you wish to restart your computer ? (yes / no): ") if restart == 'no': exit() else: os.system("rebo
import os
restart = raw_input("Do you wish to restart your computer ? (yes / no): ")
if restart == 'no':
exit()
else:
os.system("reboot", timeout(100))...something
不要使用
os.system
,而是使用subprocess.run()
:
try:
proc = subprocess.run(["reboot"], timeout=10)
except TimeoutExpired:
...
可能适合您的脚本。其他人只是简单地使用
os.system(“shutdown-r-t1”)
,其中1
表示一秒钟。我无法从你写的内容判断你是否有特权问题。我刚才试过os.system('sudo reboot')
。它起作用了。提示用户输入密码。我正在使用python 3.5+中的python 2.7不确定subprocess.run()<代码>属性错误:“模块”对象没有属性“运行”。您在2.7中看到的任何替代品?subprocess.call??在循环中使用subprocess.Popen()
和Popen.poll()
。或者我强烈建议升级到Python3,Python2已经不受支持了。