From eeb19aba418d03729721bf2f1e34e8e11368e1a5 Mon Sep 17 00:00:00 2001 From: Collin Simpson Date: Sun, 2 Aug 2020 18:38:49 -0400 Subject: [PATCH] Prevent multiple instances of sleep_weather.sh from running at once --- scripts/sleep_weather.sh | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/scripts/sleep_weather.sh b/scripts/sleep_weather.sh index 393f771..c3de174 100755 --- a/scripts/sleep_weather.sh +++ b/scripts/sleep_weather.sh @@ -4,12 +4,23 @@ 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() { + ensure_single_process + current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" $current_dir/weather.sh > $current_dir/../data/weather.txt - + while tmux has-session &> /dev/null do $current_dir/weather.sh $fahrenheit > $current_dir/../data/weather.txt @@ -20,6 +31,8 @@ main() break fi done + + rm $LOCKFILE } #run main driver function