docker 容器启动时 启动服务 开机启动 部署 ssh和其他服务
参考: http://www.163py.com/pages/122/130/657/article_index.html
把里面的run.sh换成run.py,chmod 777 run.py 修改权限; 查看日志在/root/run_python.log
启动容器, 保证容器安装了python2.7,并且吧python2.7执行放到文件头,并且安装了twisted
docker run -p 303:22 -p 3321:3321 -p 7998:7998 -p 13002:13002 -p 7208:7208 -p 7209:7209 -p 7210:7210 -p 7211:7211 -p 7212:7212 -p 7213:7213 -p 7214:7214 -p 7215:7215 -p 7217:7217 -p 7218:7218 -p 7219:7219 -p 7220:7220 -d --volumes-from dbdata --ulimit nofile=60000:60001 --ulimit nproc=10000:20000 --restart=always --name xyxy centos_xyxy:1.0 /root/run.py
#! /usr/local/bin/python2.7 #coding=utf-8 from twisted.internet import reactor from twisted.internet import threads from twisted.python import log log.startLogging(open("/root/run_python.log",'a')) log.FileLogObserver.timeFormat = '%Y-%m-%d %H:%M:%S' import os,commands class Init(object): def __init__(self): self.sshd_i = 0 self.mysqld_i = 0 self.nginx_i = 0 self.supervisord_i=0 def checkservice(self, servername): """ @des:检查服务是否存在 """ if servername not in ["sshd","mysqld","nginx","supervisord"]: return False return True def getcmdstring_start(self, servername): """ @des:获取每个服务对应的启动命令 """ if servername == "sshd": cmdstring = '/usr/sbin/sshd -D' elif servername in ["mysqld","nginx","supervisord"]: cmdstring = 'service %s start'%servername return cmdstring def getcmdstring_stop(self, servername): """ @des:获取每个服务对应的停止命令 """ if servername == "sshd": cmdstring = '/usr/sbin/sshd -D' #这里和上面一样没有问题 elif servername in ["mysqld","nginx","supervisord"]: cmdstring = 'service %s stop'%servername return cmdstring def getservicecount(self, servername): """ @des:获取每个服务的重试次数 """ old_i = getattr(self, "%s_i"%servername) new_i = old_i+1 setattr(self, "%s_i"%servername, new_i) return new_i def service(self, servername): """ @des: 启动服务 """ if not self.checkservice(servername): log.msg("check %s error"%servername) return cmdstring_start = self.getcmdstring_start(servername) cmdstring_stop = self.getcmdstring_stop(servername) new_i = self.getservicecount(servername) if new_i>=5: return (status, output) = commands.getstatusoutput('ps -ef | grep %s | grep -v grep | grep -v defunct| wc -l'%servername) #查找正常进程,排除僵死进程和grep本身 log.msg("check:", servername, status, output) if int(output)==0:#没有检测到该服务 (status, output) = commands.getstatusoutput(cmdstring_start)#开始服务 log.msg("start:", servername, status, output) reactor.callLater(1, self.service, servername)#n秒后重新检测 else: if new_i==1:#启动docker容器马上就发现有,则先要停止 (status, output) = commands.getstatusoutput(cmdstring_stop) log.msg("stop:", servername, status, output) reactor.callLater(1, self.service, servername) else:#检测到服务存在,则返回 return #end--构建服务 def qidong(): init = Init() #mysqld和supervisord有先后顺序 d = threads.deferToThread(init.service, "mysqld") d.addCallback(lambda result: init.service("supervisord")) #end--mysqld和supervisord有先后顺序 #启动其他的 reactor.callInThread(init.service, "nginx") reactor.callInThread(init.service, "sshd") #end--启动其他的 if __name__=="__main__": reactor.suggestThreadPoolSize(10) qidong() reactor.run()