zoukankan      html  css  js  c++  java
  • tomcat站点更新脚本

    tomcat站点更新脚本,欢迎吐槽

    #!/usr/local/bin/python3
    #coding:utf-8

    import os, sys, subprocess, time, shutil

    #get tommcat pid
    tomcat_pid =  ((subprocess.Popen("lsof -i :8080 |grep root |awk '{print $2}'", shell=True, stdout=subprocess.PIPE)).stdout.read()).decode()

    #关闭tomcat
    print("Tomcat will shutdown after 6s, u can enter Ctrl + c interrupt it ! ")
    for i in range(3):
        print("." ,end = "")
        sys.stdout.flush()
        time.sleep(1)

    print()
    if len(tomcat_pid) == 0:
        print("> tomcat already shutdown!")
    else:
        subprocess.Popen("/var/tomcat/bin/shutdown.sh > /dev/null 2>&1", shell=True, stdout=subprocess.PIPE)

    for i in range(3):
        print("." ,end = "")
        sys.stdout.flush()
        time.sleep(1)
        
    if len(tomcat_pid) == 0:
        pass
    else :
        subprocess.Popen("kill -9 " + tomcat_pid, shell=True, stdout=subprocess.PIPE)
        print(" > Tomcat close the failure, kill the pid %s" % tomcat_pid)
        
    #clean tomcat cache
    print(" > clean tomcat webapp cache...")
    subprocess.Popen("rm -rf /var/tomcat/temp/*", shell=True, stdout=subprocess.PIPE)
    subprocess.Popen("rm -rf /var/tomcat/work/Catalina/localhost/*", shell=True, stdout=subprocess.PIPE)
    print(" > clean tomcat webapp cache completed !!!")
       
    #备份旧站点
    print(" --------Begin to backup webapps--------- ")

    try:
        p = subprocess.Popen("Time=`date +%m%d%H%M` && tar czf /home/webbak/webapps$Time.tar.gz  /var/tomcat/webapps", shell=True, stdout=subprocess.PIPE)
        p.stdout.read()
    except Exception as e:
        print(e)

    print(" > Backup completed,Start to update the program...")    
    for i in range(3):
        print("." ,end = "")
        sys.stdout.flush()
        time.sleep(1)

    def Delete_old_site(sitefile):
        update_file = '/home/sourcedir/' + sitefile + '.war' #升级版本上传目录
        webapps_file = '/var/tomcat/webapps/' + sitefile + '.war'
        webapps_dir = '/var/tomcat/webapps/' + sitefile
        os.remove(webapps_file)
        shutil.rmtree(webapps_dir)
        if os.path.exists(webapps_file) or os.path.exists(webapps_dir):
            print(u"旧站点文件删除失败,请手动删除...")
            sys.exit(0)
        else:
            if os.path.exists(update_file):
                shutil.copyfile(update_file, webapps_file)
            else:
                sys.exit(0)
    #定义站点站点相关目录及文件位置
    site_file = sys.argv
    try:
        sitefile = site_file[1]
        Delete_old_site(sitefile)
    except Exception as e:
        print(u"warning:脚本最多指定三个参数,最少指定一个参数!!!")
        sys.exit(0)

    try:
        sitefile = site_file[2]
        Delete_old_site(sitefile)
    except Exception as e:
        pass

    try:
        sitefile = site_file[3]
        Delete_old_site(sitefile)
    except Exception as e:
        pass    

    print(" > Update completed,start tomcat...")
    subprocess.Popen("/var/tomcat/bin/startup.sh", shell=True, stdout=subprocess.PIPE)

    for i in range(3):
        print("." ,end = "")
        sys.stdout.flush()
        time.sleep(1)
    tomcat_pid2 =  ((subprocess.Popen("lsof -i :8080 |grep root |awk '{print $2}'", shell=True, stdout=subprocess.PIPE)).stdout.read()).decode()

    if len(tomcat_pid2) == 0:
        print(" > Tomcat has not start, Please check u program!")
    else:
        print(" > Tomcat have already started !")

    END!

  • 相关阅读:
    机器学习-数据表达与特征工程
    机器学习-朴素贝叶斯算法
    机器学习之广义线性模型
    机器学习-MNIST数据集使用二分类
    机器学习-数据预处理、降维、特征提取及聚类
    机器学习-神经网络
    机器学习-向量机SVM
    机器学习-决策树与随机森林
    深入计算机原理——第三章机器级的表示(三)
    深入计算机原理——第三章机器级的表示(二)
  • 原文地址:https://www.cnblogs.com/changbo/p/6423784.html
Copyright © 2011-2022 走看看