网站制作学习网Linux→正文:获取shell 后台执行进程号
字体:

获取shell 后台执行进程号

Linux 2023/10/20 15:19:11  点击:不统计

forasp.cn

 在linux 中经常使用 在后台执行某段代码,比如

python test.py > test.log 2>&1 &
 
如果每次结束时候要么自动结束,要么通过 ps aux|grep test.py 找到pid 
kill pid 结束进程。
 
那通过自动获取 后台运行时的进程号,再 写个自动kill 的shell 脚本呢。
先看 启动运行shell脚本 start.sh

#!/bin/bash
# 这里是清空文件
echo :>.test.pid.txt
# 后台运行命令,将输出到test.log 日志
python test.py > test.log 2>&1 &
# 这里是获取最近的一个进程ID 输出到上面文件中
echo $! >.test.pid.txt
结束日志脚本 stop.sh 

#!/bin/bash
# 读取pid.txt文件中的进程号
pid=$(cat .test.pid.txt)
# 杀死进程
kill $pid


·上一篇:ubuntu 初始化配置 >>    ·下一篇:protoc-gen-grpc-web: program not found or is not executable >>
推荐文章
最新文章