Skip to content

Commit

Permalink
Fixed ability to run multiple instances.
Browse files Browse the repository at this point in the history
  • Loading branch information
MyCatShoegazer committed Feb 13, 2018
1 parent 0a76301 commit c29b866
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions fanboost.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ function check_root() {
fi
}

# Checks if process is already running
function mutex() {
local file=$1 pid pids

exec 9>>"$file"
{ pids=$(fuser -f "$file"); } 2>&- 9>&-
for pid in $pids; do
[[ $pid = $$ ]] && continue

exec 9>&-
return 1 # Locked by a pid.
done
}

# Main condition sequence
if [ "$1" = "--on" ]
then
Expand All @@ -83,6 +97,9 @@ then
# Call root cheking
check_root "$@"

# If script with this option is already running then exit
mutex /var/run/mac_fan_booster.lock || { echo "$SCRIPT_NAME is already running!" >&2; exit 1; }

# Write boosting enabled event to syslog
logger -t $SCRIPT_NAME "Boosting fans..."

Expand Down Expand Up @@ -110,8 +127,8 @@ then
echo 0 > $FAN_1$MANUAL_OUT
echo 0 > $FAN_2$MANUAL_OUT

# Call script itself with -v arg
"$0" -v
# Kill all instances of script
pkill -9 -f "$0"
elif [ "$1" = "-v" ]
then
# If -v argument is provided
Expand Down Expand Up @@ -144,6 +161,9 @@ then
# Call root cheking
check_root "$@"

# If script with this option is already running then exit
mutex /var/run/mac_fan_booster.lock || { echo "$SCRIPT_NAME is already running!" >&2; exit 1; }

# Temperature after which cooling will be enabled
TRESHOLD_TEMP=$2

Expand Down

0 comments on commit c29b866

Please sign in to comment.