Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pidfile is not updated when daemon is started #328

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions battery.sh
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ function valid_voltage() {
return 1
}

function kill_daemon() {
if test -f "$pidfile"; then
log "Killing old maintain process at $(cat $pidfile)"
pid=$(cat "$pidfile" 2>/dev/null)
kill $pid &>/dev/null
fi
}

## #################
## SMC Manipulation
## #################
Expand Down Expand Up @@ -510,6 +518,8 @@ fi

# Maintain at level
if [[ "$action" == "maintain_synchronous" ]]; then
kill_daemon
echo $$ >$pidfile

# Checking if the calibration process is running
if test -f "$calibrate_pidfile"; then
Expand Down Expand Up @@ -589,6 +599,8 @@ fi

# Maintain at voltage
if [[ "$action" == "maintain_voltage_synchronous" ]]; then
kill_daemon
echo $$ >$pidfile

# Recover old maintain status if old setting is found
if [[ "$setting" == "recover" ]]; then
Expand Down Expand Up @@ -638,21 +650,15 @@ fi
# Asynchronous battery level maintenance
if [[ "$action" == "maintain" ]]; then

# Kill old process silently
if test -f "$pidfile"; then
log "Killing old maintain process at $(cat $pidfile)"
pid=$(cat "$pidfile" 2>/dev/null)
kill $pid &>/dev/null
fi

if test -f "$calibrate_pidfile"; then
pid=$(cat "$calibrate_pidfile" 2>/dev/null)
kill $calibrate_pidfile &>/dev/null
log "🚨 Calibration process have been stopped"
fi

if [[ "$setting" == "stop" ]]; then
log "Killing running maintain daemons & enabling charging as default state"
kill_daemon
log "Enabling charging as default state"
rm $pidfile 2>/dev/null
$battery_binary disable_daemon
enable_charging
Expand Down Expand Up @@ -702,8 +708,6 @@ if [[ "$action" == "maintain" ]]; then
nohup $battery_binary maintain_synchronous $setting $subsetting >>$logfile &
fi

# Store pid of maintenance process and setting
echo $! >$pidfile
pid=$(cat "$pidfile" 2>/dev/null)

if ! [[ "$setting" == "recover" ]]; then
Expand Down