Skip to content

Instantly share code, notes, and snippets.

@windwiny
Last active May 16, 2017 14:24
Show Gist options
  • Save windwiny/00a47cef93110e99c16859aea12377a2 to your computer and use it in GitHub Desktop.
Save windwiny/00a47cef93110e99c16859aea12377a2 to your computer and use it in GitHub Desktop.
shell create pid file , avoid script running multiple times
#!/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