shell腳本如何防止重復(fù)執(zhí)行,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
嵐皋ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)公司的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!代碼如下:
#!/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
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
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
防止腳本多次執(zhí)行(一)
代碼如下:
while [ `ps x |grep -v grep|grep filename.sh|wc -l` -gt 0 ];
do
exit
done
看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進(jìn)一步的了解或閱讀更多相關(guān)文章,請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,的支持。