python小脚本-restart tomcat
#!/usr/bin/python#-*-coding:utf-8-*-'''重启tomcat'''import osdef checkProcess(pid): ''' check process if exist''' if len(os.popen(r"ps -ef |grep -e '^\w*\s*"+pid+"'").read())==0: return False else: return Trueif os.name=='nt':#不支持windows print 'sorry,this script dont support window!'else: curusr=os.popen("whoami").read() exdi=raw_input("input tomcat's bin directory:(/usr/local/tomcat)") if len(exdi)==0: exdi='/usr/local/tomcat' tmp=os.popen("ps -ef | grep java|grep -c "+exdi).read().strip() #返回grep后的行数,如果返回一行,就返回grep的进程,tomcat没有启动 if tmp=='1':#用户指定的tomcat没启动 print exdi,' dont start' else:#用户指定的tomcat已启动 tmp=os.popen("ps -ef | grep java|grep "+exdi).read().strip() l=tmp.split() exusr=l[0]#tomcat的执行用户 pid=l[1] print "tomcat's bin directory:",exdi print 'exeusr:',exusr,' pid',pid print 'execute shutdown.sh' flag=False#判断是否执行成功 if os.system(exdi+"/bin/shutdown.sh")==0: if not checkProcess(pid): print 'shut down tomcat success' flag=True else: print 'shutdown.sh executed,but process not be killed' flag=False else: flag=False while not flag: os.system("kill "+pid) if not checkProcess(pid): flag=True print 'tomcat kill success' print exusr.strip()==curusr.strip(),exusr.strip(),curusr.strip() if exusr.strip()==curusr.strip(): if os.system(exdi+'/bin/startup.sh')==0: print 'start up tomcat success' else: if os.system('su - '+exusr)==0: if os.system(exdi+'/bin/startup.sh')==0: print 'start up tomcat success' print os.popen("ps -ef | grep java|grep "+exdi).read() #让用户检查结果?