python kill process两种思路

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/windy135/article/details/85425400

使用subprocess库执行shell操作。

一、直接使用shell杀进程

import subprocess
subprocess.check_output("for p in `lsof -n -i:8080 | grep LISTEN | awk '{print $2}'`; do kill -9 $p; done", shell=True)

二,先拿到进程号,在kill

port = subprocess.check_output("lsof -n -i:8080 | grep LISTEN | awk '{print $2}'", shell=True).decode().strip('\n')
        if port:
            subprocess.check_output('kill -9 {}'.format(port), shell=True)

猜你喜欢

转载自blog.csdn.net/windy135/article/details/85425400