zoukankan      html  css  js  c++  java
  • bash: 避免命令重复执行的简单脚本

    1. 根据命令生成md5做为文件名保存当前进程的pid
    2. 使用exec执行命令
    3. 如果再次执行, 使用ps -p检测上次pid是否有效, 如果是则exit 200.否则重复1.
    hadoop@ubuntu102:~$ cat guard
    #!/bin/bash
    declare fpid=/tmp/$(echo -n "$@"|md5sum|awk '{print $1}');
    if [[ -f ${fpid} ]];then
    read pid < ${fpid} && ps -p ${pid:-0} > /dev/null && if [[ $? -eq 0 ]];then echo "previous process still running...${pid}, $*" && exit 200; fi
    fi
    echo $$ > ${fpid}
    exec $*
    hadoop@ubuntu102:~$ ./guard sleep 10 &
    [1] 4070
    hadoop@ubuntu102:~$ ./guard sleep 10
    previous process still running...4070, sleep 10
    hadoop@ubuntu102:~$ ./guard sleep 10
    previous process still running...4070, sleep 1

  • 相关阅读:
    递归
    匿名函数
    迭代器、可迭代对象、生成器
    日期
    大文件读写
    面向对象
    魔术方法
    进程与线程
    numpy常用函数
    shell编程
  • 原文地址:https://www.cnblogs.com/zolo/p/5849259.html
Copyright © 2011-2022 走看看