1. 根据命令生成md5做为文件名保存当前进程的pid
2. 使用exec执行命令
3. 如果再次执行, 使用ps -p检测上次pid是否有效, 如果是则exit 200.否则重复1.
[email protected]:~$ cat guard
#!/bin/bash
declare fpid=/tmp/$(echo -n "[email protected]"|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 $*
[email protected]:~$ ./guard sleep 10 &
[1] 4070
[email protected]:~$ ./guard sleep 10
previous process still running...4070, sleep 10
[email protected]:~$ ./guard sleep 10
previous process still running...4070, sleep 1
时间: 2024-10-20 17:13:00