ps aux | grep test | grep -v grep

import subprocess

CHECK_SCRIPT = "test.py"

# 过滤 master_ip
waf_ip_list = filter(lambda x: x!= master_ip, set(waf_ips.split(',')))
waf_ips = ",".join(waf_ip_list)

# 检查进程CHECK_SCRIPT是否在运行
p = Popen("ps aux | grep {} | grep -v grep".format(CHECK_SCRIPT), stdout=PIPE,shell=True)
output_p = p.stdout.readlines()
if output_p:
    return JsonResponse({'code': 0, 'msg': '脚本正在运行,请稍后再试'})
else:
    cmd = 'python {}'.format(CHECK_SCRIPT)
    res = subprocess.Popen(cmd, shell=True, 
                           stdout=subprocess.PIPE, 
                           stderr=subprocess.PIPE)
    err = res.stderr.read()
    if error:
        cmd_res = err
    else:
        cmd_res = res.stdout.read()
    return JsonResponse({'code': 0, 'msg': cmd_res)

知识点

  1. 灵活使用高阶函数 filter(), lambda(), map(), reduce()

  2. ps aux | grep test.py | grep -v grep

    grep :查找含有指定字段的行
    grep -v :反向查找,查找不含有指定字段的行

    在这里,我想查看test.py是否在运行,使用grep命令进行过滤查找,而grep本身也是一个进程,所以需要把grep自身的进程过滤掉

    例如:cat test.log | grep "login"|grep -v "deviceType"

    找出test.log中包含login信息的,且没有deviceType这个字段的

  3. 利用管道机制subprocess.PIPE实现非阻塞模式

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值