import os,stat base_dir = "/Users/xmx/Desktop/app/redis" port=7000 num=6 os.chdir(base_dir) for i in range(num): dirs = os.path.join(base_dir,str(port + i)) os.mkdir(dirs) f = open(dirs+'/redis.conf', 'w+') f.write("daemonize yes ") f.write("port "+str(port+i)+" ") f.write("cluster-enabled yes ") f.write("cluster-config-file nodes.conf ") f.write("cluster-node-timeout 5000 ") f.write("appendonly yes ") f.write("dir " + dirs+" ") f.close() start_sh = os.path.join(base_dir, "redis_start.sh") stop_sh = os.path.join(base_dir, "redis_stop.sh") f=open(start_sh,"w+") f.write("#!/bin/bash ") f.write("base_dir="+base_dir+" ") f.write(" ") for i in range(num): f.write('redis-server "${base_dir}/'+str(port+i)+'/redis.conf" ') f.close() f = open(stop_sh, "w+") f.write("#!/bin/bash ") f.write(" ") for i in range(num): f.write("redis-cli -p "+str(port+i)+" shutdown ") f.close() os.chmod(start_sh, stat.S_IRWXU | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) os.chmod(stop_sh, stat.S_IRWXU | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)