实现同一个脚本多次运行,系统中只有一个进程


编写脚本,实现同一个脚本多次运行,系统中只有一个进程

root@nfs scripts]#cat pid.sh

#!/bin/sh
pidpath=/tmp/a.pid
if [ -f "$pidpath" ]
  then
    kill `cat $pidpath` >/dev/null 2>&1
    rm -f $pidpath
fi

echo $$ >$pidpath
sleep 300


测试如下

root@nfs scripts]# ps -ef|grep pid.sh|grep -v grep

[root@nfs scripts]# sh pid.sh &

[1] 3883

[root@nfs scripts]# ps -ef|grep pid.sh|grep -v grep

root       3883   1297  0 20:36 pts/0    00:00:00 sh pid.sh

[root@nfs scripts]# sh pid.sh &

[2] 3890

[root@nfs scripts]# sh pid.sh &

[3] 3894

[1]   Terminated              sh pid.sh

[root@nfs scripts]# sh pid.sh &

[4] 3898

[2]   Terminated              sh pid.sh

[root@nfs scripts]# ps -ef|grep pid.sh|grep -v grep

root       3898   1297  0 20:36 pts/0    00:00:00 sh pid.sh

[3]-  Terminated              sh pid.sh




来源:春哥技术博客,欢迎分享,转载请注明出处。(欢迎加春哥团队客服微信号:taike668)

本文地址:https://www.cgtblog.com/jishu/1761.html
上一篇:小微头条网内容页模板代码分享      下一篇:MS17-010 "Eternal Blue(永恒之蓝)”, 修