Created
December 19, 2014 02:04
-
-
Save mk0x9/ca4b849758f7b7bcc89d to your computer and use it in GitHub Desktop.
getmail with lockfile
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
#!/usr/bin/env bash | |
me=`basename $0` | |
lockfile="/tmp/$me.lock" | |
lock () { | |
if [ "$1" = "lock" ] | |
then | |
if [ -e "$lockfile" ] | |
then | |
>&2 echo "$lockfile already exists" | |
exit 1 | |
fi | |
touch "$lockfile" | |
fi | |
if [ "$1" = "unlock" ] | |
then | |
rm "$lockfile" | |
fi | |
} | |
handler () { | |
kill -s SIGTERM $PID | |
} | |
lock "lock" | |
getmail $@ & | |
PID=$! | |
trap handler SIGINT | |
while $(kill -0 $PID 2>/dev/null) | |
do | |
sleep 1 | |
done | |
lock "unlock" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment