Python启动、重试失败服务
说明:
- 脚本批量启动服务
- 如果服务未启动,则重试,并等待60s
- 对于启动失败的服务,最多重试10次
- 最终输出启动失败的服务
代码如下:
#!/usr/bin/python
# _*_ coding: UTF-8 _*_
import os
from time import sleep
import time
## 脚本主要是用来批量启动服务,如果服务未启动,则重试它,并等待60s,对于启动失败的服务,最多尝试10次。
## 并最终输出启动失败的服务
class Service:
def __init__(self,name):
self.name = name
self.isBoot = False
def isBoot(self):
return self.isBoot
def boot(self):
os.system("systemctl start %s" % self.name) #如使用systemctl start self.name启动服务
def checkWhetherBoot(service):
name = service.name
es = os.system("ps -ef | grep %s | grep -v grep" % name)
if es == 0:
service.isBoot = True
# 比如有4个服务nginx, mysql, redis, mongodb
nginx = Service("nginx")
mysql = Service("mysql")
redis = Service("redis")
mongodb = Service("mongodb")
print("Start time: {}", time.ctime(time.time()))
serviceList = [nginx, mysql, redis, mongodb]
for ser in serviceList:
checkWhetherBoot(ser)
if not ser.isBoot:
ser.boot()
sleep(10)
sleep(30)
failedService = list(serviceList)
times = 10
i = 0
while i < times:
for ser in failedService:
checkWhetherBoot(ser)
if ser.isBoot:
failedService.remove(ser)
else:
ser.boot()
sleep(60)
checkWhetherBoot(ser)
if ser.isBoot:
failedService.remove(ser)
if not failedService:
break
i += 1
for ss in failedService:
print("Failed service: {}", ss.name)
print("End time: {}", time.ctime(time.time()))
总结:
- 如果需要新增启动服务,需要新创建Service实例
- Service的boot方法有很大的改进空间,因为不同Service的启动肯定不都是
systemctl start xxx
,这里是为了方便就这样展示了。可以尝试将Service启动参数加到__init__
的参数里。
\
改进版本0.2
对上面提到的第二点进行了改进,这样我们就可以自由定义每个服务如何启动了,更加通用了
代码如下
#!/usr/bin/python
# _*_ coding: UTF-8 _*_
import os
from time import sleep
import time
## 脚本主要是用来批量启动服务,如果服务未启动,则重启它,并等待60s,对于启动失败的服务,最多尝试10次。
## 并最终输出启动失败的服务
class Service:
def __init__(self,name,bootCommand): #指定启动命令
self.name = name
self.isBoot = False
self.bootCommand = bootCommand
def isBoot(self):
return self.isBoot
def boot(self):
os.system("%s" % self.bootCommand)
def checkWhetherBoot(service):
name = service.name
es = os.system("ps -ef | grep %s | grep -v grep" % name)
if es == 0:
service.isBoot = True
# 比如有4个服务nginx, mysql, redis, mongodb
nginx = Service("nginx", "systemctl start nginx")
mysql = Service("mysql", "systemctl start mysql")
redis = Service("redis", "redis-server redis.conf")
mongodb = Service("mongodb", "systemctl start mongodb")
print("Start time: {}", time.ctime(time.time()))
serviceList = [nginx, mysql, redis, mongodb]
for ser in serviceList:
checkWhetherBoot(ser)
if not ser.isBoot:
ser.boot()
sleep(10)
sleep(30)
failedService = list(serviceList)
times = 10
i = 0
while i < times:
for ser in failedService:
checkWhetherBoot(ser)
if ser.isBoot:
failedService.remove(ser)
else:
ser.boot()
sleep(60)
checkWhetherBoot(ser)
if ser.isBoot:
failedService.remove(ser)
if not failedService:
break
i += 1
for ss in failedService:
print("Failed service: {}", ss.name)
print("End time: {}", time.ctime(time.time()))