-1

我想使用 python 重新启动远程 linux(Redhat) 机器,但有时远程 pc 在重新启动过程中卡住了。如果重启时间太长,我是否可以超时?即如果需要超过 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
4

1 回答 1

1

不要使用os.systemsubprocess.run()而是使用:

try:
    proc = subprocess.run(["reboot"], timeout=10)
except TimeoutExpired:
    ...
于 2020-09-22T05:22:12.867 回答