Last active
May 16, 2017 14:24
-
-
Save windwiny/00a47cef93110e99c16859aea12377a2 to your computer and use it in GitHub Desktop.
shell create pid file , avoid script running multiple times
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
## create pid file , avoid script running multiple times | |
### every script gen a unique UUID | |
UUID=ACD35429D76148978480EA945653D112 | |
pid_file=/tmp/${UUID}.pid | |
if [[ -f $pid_file ]]; then | |
lpid=$(head -1 $pid_file) | |
if [[ ! -z $lpid ]]; then | |
if [[ $(ps -ef | grep 'sh ' | awk '{print $2}' | egrep "^${lpid}$") ]]; then | |
echo "same script has running ... pid is $lpid , exit ..." | |
exit 0 | |
fi | |
fi | |
fi | |
echo $$ > $pid_file | |
## content | |
sleep 10 | |
## last remove pid file | |
rm -f $pid_file | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment