#!/usr/bin/env python
# -*- coding: utf-8 -*-
import redis,threading,sys,socket,time,os
from multiprocessing import Pool
def port_check(host,port):
POOL = redis.ConnectionPool(host=host, port=port)
my_server = redis.Redis(connection_pool=POOL)
try:
a = my_server.ping()
if a:
return 1
else:
return 0
except Exception,e:
return 0
def shutdown_server(host,port):
flag = port_check(host,port)
if flag == 1:
POOL = redis.ConnectionPool(host=host, port=port)
my_server = redis.Redis(connection_pool=POOL)
my_server.shutdown()
while 1:
flag = port_check(host,port)
if flag == 0:
print "%s:%s is Shutdown OK" % (host,port)
break
else:
time.sleep(1)
else:
print "%s:%s is Shutdown alreaday" % (host,port)
def start_server(host,port):
flag = port_check(host,port)
if flag == 0:
start_conm = "/usr/local/bin/redis-server /usr/local/redis/etc/redis_%s.conf" % port
os.popen(start_conm)
time.sleep(3)
i = 0
while 1:#每5s检测一次redis ping,持续一分钟,1分钟没有起来认为启动失败
flag = port_check(host,port)
if flag == 1:
print "%s:%s is Start OK" % (host,port)
break
else:
if i > 12:
print "%s:%s is Start Error" % (host,port)
break
else:
time.sleep(5)
i = i + 1
else:
print "%s:%s is Start alreaday" % (host,port)
def restart(host,port):
shutdown_server(host,port)
start_server(host,port)
def main():
server_list = ["127.0.0.1:6379","127.0.0.1:16379"]
pool = Pool(processes=3)
for i in server_list:
aa = i.split(':')
host = aa[0]
port = int(aa[1])
results = pool.apply_async(restart, (host,port))
pool.close()
pool.join()
if results.successful():
print 'successful'
if __name__=="__main__":
main()