python语言copy的使用,服务进程ID的获取和停止进程

本文详细介绍了一种自动化部署和管理Java应用的具体方法,包括如何使用脚本复制jar包到指定目录,通过wmic和taskkill命令自动化地查找并关闭运行中的应用进程,最后重新启动更新后的服务。

# -*- coding: utf-8 -*-
import shutil
import os

file = 'D:\\Program Files (x86)\\Jenkins\\workspace\\maven_server\\coe-server\\target\\coe-server-0.0.1-SNAPSHOT.jar'
path = 'D:\\COE\\coe-service\\coe-server\\'

shutil.copy(file,path)

print('---------copy jar包 to coe-server  ----')

#关闭coe-server
print ("获取coe-server的进程ID")
ip_path = 'D:\\COE\\coe-service\\coe-server\\ip.txt'  #wmic process获取processid后,将返回结果存放到txt
os.system('wmic process where commandline="java  -server -Xms1024m -Xmx20480m -jar coe-server-0.0.1-SNAPSHOT.jar" get processid /value  > D:\\COE\\coe-service\\coe-server\\ip.txt')

#获取的txt格式不是utf-8,需要转换type
os.system('type D:\\COE\\coe-service\\coe-server\\ip.txt>D:\\COE\\coe-service\\coe-server\\ip2.txt')

#从txt中分割字符串,获取id值
with open('D:\\COE\\coe-service\\coe-server\\ip2.txt') as file_obj:
    content = file_obj.read()
    print(content.rstrip())
    content = content.rstrip()
    if content != None:
        try:
            id = content.split('=')[1]
            print('id:'+id)
            print ('taskkill /IM '+id+' /F')
            os.system('taskkill /IM '+id+' /F')
            print ('coe-server进程已关闭')
        except Exception as e:
            print ('coe-server进程已关闭',e)
    else:
        print ('coe-server进程已关闭')

 

 

 

---------------------------------

启服务命令:

java -jar coe-server-0.0.1-SNAPSHOT.jar
start /b java -server -Xms1024m -Xmx20480m -jar coe-server-0.0.1-SNAPSHOT.jar

windows查看java进程
wmic process   查看所有
wmic process get name,processid,caption,parentprocessid,threadcount/value 
wmic process where caption="java.exe" get processid,caption,commandline /value
wmic process where commandline="java  -server -Xms1024m -Xmx20480m -jar coe-server-0.0.1-
SNAPSHOT.jar" get name,processid,caption,commandline /value

wmic process where commandline="java  -server -Xms1024m -Xmx20480m -jar coe-server-0.0.1-
SNAPSHOT.jar" get processid /value  > D:\\COE\\coe-service\\coe-server\\ip.txt  将输入内容存放到txt

使用 进程名关闭
taskkill /im mspaint.exe /f

使用 进程id 关闭
taskkill /im 12555 /f

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值