例如,要求脚本只能顺序访问某个资源,例如磁盘文件等,就可以参考下面的实现。
复制代码 代码如下:
#!/bin/bash
#
# file locking using bash.
# ver 0.1.6
#
# author : malundao ( malundao@sina.com )
# date : 2011-08-31
# ref : http://unix.derkeiler.com/Newsgroups/comp.unix.shell/2005-09/0472.html
#
# note:
# shflock_cleanhook() is a user defined function to clean up user-specific sth.
#
# /path/to/lock/. note; directory, not a file.
# should be modified
LOCKPATH="/tmp"
cleanup() {
shflock_cleanhook
cd $LOCKPATH
[ -e lock.pid ] || exit
read pid >/dev/null 2>&1 <lock.pid
if [ -n "$pid" ]; then
if [ "$pid" == "$$" ]; then
rm -f lock.$pid
rm -f lock.pid
exit
fi
fi
exit
}
# trap EXIT ?
trap 'cleanup' HUP INT TERM
getlock() {
oldpath=`pwd`
cd $LOCKPATH
while
echo $$ > lock.$$
[ -e lock.pid ]
do
rm lock.$$
read pid >/dev/null 2>&1 <lock.pid
if [ -n "$pid" ]; then
if [ -e /proc/$pid ]; then
cd $oldpath
return 1 # Lock is taken by others
else
#unsafe: rm -f lock.$pid
echo WARN: please delete stale lock.pid by HAND.
return 2
fi
else
# sleep some seconds,then back to 'while' loop
# 11 is a prime number, $$ as a random.
echo sleep $(( $$ % 11 ))
sleep $(( $$ % 11 ))
fi
done
# 'ln -s' is an atom op.
ln -s lock.$$ lock.pid >/dev/null 2>&1
if [ $? -eq 0 ]; then
cd $oldpath
return 0 # We got the lock
else
[ -e lock.pid ] || echo WARN: please delete hanging lock.pid by HAND.
cd $oldpath
return 3 # Lock is probably taken by others.
fi
}
putlock () {
oldpath=`pwd`
cd $LOCKPATH && rm -f lock.$$ lock.pid
cd $oldpath
}
## test
shflock_cleanhook() {
echo i\'m a hook.
}
while true; do
while ! getlock; do
#echo wait a second...
sleep 1
done
echo \[$$\] `date` ,now hold lock for 3 seconds...
sleep 3
#echo putlock
putlock
sleep 1 # yield
done
防止脚本多次执行(一)
复制代码 代码如下:
while [ `ps x |grep -v grep|grep filename.sh|wc -l` -gt 0 ];
do
exit
done
把内容放在脚本最上面
filename.sh 替换为你要监控的脚本名字
还有一种方法是 死循环 可以根据不同的环境,不同的脚本内容,使用的方法也同步,方法很简单日后在写出来
脚本重复执行
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]