Ok I need just some help. I'm on a debian linux device. I need a script / service that…
- User
Ok I need just some help. I'm on a debian linux device. I need a script / service that is always running (and autostarted at boot, possibly) that checks if the power supply is connected. I would prefer if you just used bash, but if there is no other way you can use python too to do this. If you can do both, propose me both your solutions
I can check that with the command: cat /sys/class/power_supply/battery/status The output of this command can be: "Full" : the charger is connected "Charging" : the charger is connected "Discharging" : the charger is NOT connected
The service should check if the charger is connected or not and, depending on the result of this check, act this way: If the charger is connected then do nothing and just wait a minute before checking again. If the charger is NOT connected then start a 10 minute timer. While the timer is running, still do the check every minute and, depending on the check: if the power has been plugged back in, discard the 10 minute timer. if the power is still unplugged, keep the timer running. If the timer ever runs out, shutdown the device
- Assistant
I will provide you with both a bash and a python solution. You can choose the one you prefer.
Bash Solution:
- Create a script file named "check_power.sh" and paste the following code:
bash#!/bin/bash counter=0 while true; do status=$(cat /sys/class/power_supply/battery/status) if [[ "$status" == "Discharging" ]]; then counter=$((counter+1)) else counter=0 fi if [[ $counter -ge 10 ]]; then sudo shutdown -h now fi sleep 60 done- Make the script executable: