Prevent multiple instances of sleep_weather.sh from running at once

This commit is contained in:
Collin Simpson 2020-08-02 18:38:49 -04:00 committed by xunoaib
parent 4f3bf8a9be
commit eeb19aba41

View file

@ -4,12 +4,23 @@
fahrenheit=$1 fahrenheit=$1
LOCKFILE=/tmp/.dracula-tmux-weather.lock
ensure_single_process()
{
# check for another running instance of this script and terminate it if found
[ -f $LOCKFILE ] && ps -p "$(cat $LOCKFILE)" -o cmd= | grep -F " ${BASH_SOURCE[0]}" && kill "$(cat $LOCKFILE)"
echo $$ > $LOCKFILE
}
main() main()
{ {
ensure_single_process
current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
$current_dir/weather.sh > $current_dir/../data/weather.txt $current_dir/weather.sh > $current_dir/../data/weather.txt
while tmux has-session &> /dev/null while tmux has-session &> /dev/null
do do
$current_dir/weather.sh $fahrenheit > $current_dir/../data/weather.txt $current_dir/weather.sh $fahrenheit > $current_dir/../data/weather.txt
@ -20,6 +31,8 @@ main()
break break
fi fi
done done
rm $LOCKFILE
} }
#run main driver function #run main driver function