Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果使用python卡滞,请尝试清除重新启动并超时_Python - Fatal编程技术网

如果使用python卡滞,请尝试清除重新启动并超时

如果使用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

我想用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("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已经不受支持了。