Skip to content

Instantly share code, notes, and snippets.

@mk0x9
Created December 19, 2014 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mk0x9/ca4b849758f7b7bcc89d to your computer and use it in GitHub Desktop.
Save mk0x9/ca4b849758f7b7bcc89d to your computer and use it in GitHub Desktop.
getmail with lockfile
#!/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