while True : ret = os.system("python3 /home/pi/Documents/8-17/simplest.py") if ret == 0: # simplest.py 运行成功,退出循环 print("simplest运行正常.....") break程序不会运行if判断
时间: 2023-12-10 09:38:17 浏览: 30
你可以在运行 `os.system()` 命令的时候将返回值存储在变量中,然后再进行判断。例如:
```
while True:
ret = os.system("python3 /home/pi/Documents/8-17/simplest.py")
if ret == 0:
print("simplest运行正常.....")
break
```
这样,如果 `simplest.py` 运行成功,`os.system()` 命令会返回 0,然后程序就会执行 `if` 语句块中的代码。如果 `simplest.py` 运行失败,`os.system()` 命令会返回非零值,程序就会继续执行 `while` 循环,直到 `simplest.py` 运行成功为止。
相关问题
if chinanet is False: print("疑似存在网络异常") getNet() else: print("网络正常") while True: ret = os.system("python3 /home/pi/Documents/8-17/simplest.py") if ret == 0: # simplest.py 运行成功,退出循环 break else: # simplest.py 运行失败,判断是否是网络中断导致的 chinanet = isNetChainOK() if chinanet is False: print("simplest.py 运行失败,且网络异常,尝试重新连接网络") getNet() else: print("simplest.py 运行失败,重新启动 simplest.py") 如果网络中断 关闭simplest,py
可以在代码中加入判断网络状态的函数并在网络异常时关闭 simplest.py,可以参考以下代码:
```
import os
import time
import requests
def isNetChainOK():
try:
requests.get("http://www.baidu.com", timeout=2)
return True
except:
return False
def getNet():
os.system("sudo ifconfig wlan0 down")
time.sleep(1)
os.system("sudo ifconfig wlan0 up")
time.sleep(1)
os.system("sudo dhclient wlan0")
chinanet = isNetChainOK()
if chinanet is False:
print("疑似存在网络异常")
getNet()
else:
print("网络正常")
while True:
ret = os.system("python3 /home/pi/Documents/8-17/simplest.py")
if ret == 0:
# simplest.py 运行成功,退出循环
break
else:
# simplest.py 运行失败,判断是否是网络中断导致的
chinanet = isNetChainOK()
if chinanet is False:
print("simplest.py 运行失败,且网络异常,关闭 simplest.py")
os.system("sudo pkill -f simplest.py")
break
else:
print("simplest.py 运行失败,重新启动 simplest.py")
```
这样,在运行 simplest.py 时如果出现网络异常导致运行失败,程序会关闭 simplest.py 并退出循环。
while True : ret = os.system("python3 /home/pi/Documents/8-17/simplest.py") if ret == 0: if chinanet is False: print("网络异常") if any(p.name() == 'simplest.py' for p in psutil.process_iter()): print("网络异常,中断simplest.py") # 中断 simplest.py os.system("pkill -f simplest.py") # simplest.py 运行成功,退出循环 break else: # simplest.py 运行失败,判断是否是网络中断导致的 chinanet = isNetChainOK() if chinanet is False: print("simplest.py 运行失败,且网络异常,尝试重新连接网络") getNet() else: print("simplest.py 运行失败,重新启动 simplest.py") 我想让网络异常时中断正在运行的simplest.py
你可以在判断网络异常的部分加上中断 simplest.py 的代码。具体实现方式可以使用 psutil 模块来获取正在运行的进程列表,然后判断是否有 simplest.py 进程在运行,如果有则使用 os.system("pkill -f simplest.py") 来中断进程。示例代码如下:
```python
import os
import psutil
while True:
ret = os.system("python3 /home/pi/Documents/8-17/simplest.py")
if ret == 0:
if chinanet is False:
print("网络异常")
if any(p.name() == 'simplest.py' for p in psutil.process_iter()):
print("网络异常,中断simplest.py")
# 中断 simplest.py
os.system("pkill -f simplest.py")
# simplest.py 运行成功,退出循环
break
else:
# simplest.py 运行失败,判断是否是网络中断导致的
chinanet = isNetChainOK()
if chinanet is False:
print("simplest.py 运行失败,且网络异常,尝试重新连接网络")
getNet()
if any(p.name() == 'simplest.py' for p in psutil.process_iter()):
print("网络异常,中断simplest.py")
# 中断 simplest.py
os.system("pkill -f simplest.py")
else:
print("simplest.py 运行失败,重新启动 simplest.py")
```