Jump to content

Recommended Posts

Posted

Dear all,

 

My small server based on the Cubietruck board and Armbian OS (ARMBIAN 5.38 stable Debian GNU/Linux 9 (stretch) 4.14.18-sunxi) runs flawlessly since over 2.5 years now. And I am very happy with this setup.

 

Lately, I installed a battery (5.3 Ah 3.7 V) in order to have the server a bit portable. The battery. driven system runs approx 9 - 10 h. Since I want to prevent that the battery drains out completely I configured the following cronjob:

*/15  *  *  *  * /home/ncuser/nc_batt_cap_shutdown.sh > /dev/null 2>&1

The content of the script looks as follows:

#!/bin/bash

### Script idea taken from:
# http://www.schulz-alex.de/cms/index.php/homeautomation-homecontrol/hardwareplattformen/cubietruck/59-akku-fuer-cubietruck

LOG_NEW=""
LOG_FILE=/home/ncuser/battery_cap_shutdown.log
BATPARAM=`cat /sys/class/power_supply/axp20x-battery/uevent`
source /sys/class/power_supply/axp20x-battery/uevent
AC_POWER_SUPPLY_PRESENT=`cat /sys/class/power_supply/axp20x-ac/present`

### Execute script only if AC adapter is not plugged in
if [ ${AC_POWER_SUPPLY_PRESENT} -eq 0 ]; then
### No AC adapter plugged in
   if [[ ${POWER_SUPPLY_PRESENT} -eq 0 || ${POWER_SUPPLY_ONLINE} -eq 0 ]]; then
### If no battery installed or battery is not discharged
      exit
   fi
   if [[ ${POWER_SUPPLY_ONLINE} -eq 1 && ${POWER_SUPPLY_CAPACITY} -le 10 ]]; then
      if [ ! -f "$LOG_FILE" ]; then
### Create log file if it is not yet there
         touch $LOG_FILE
      fi
      LOG_NEW+="$(date +'%d.%m.%Y %H:%M:%S')\tBattery capacity too low: ${POWER_SUPPLY_CAPACITY}\n"
      LOG_NEW+="$(date +'%d.%m.%Y %H:%M:%S')\tOutput server battery state:\n${BATPARAM}\n"
      LOG_NEW+="$(date +'%d.%m.%Y %H:%M:%S')\tShut down system\n"
      SUBJECT="[Server] - Shutdown - Battery capacity too low: ${POWER_SUPPLY_CAPACITY}"
      TO="email@domain.tld"
      LOG_NEW+="$(date +'%d.%m.%Y %H:%M:%S')\tSend email with battery state info to: ${TO}\n"
      printf "$LOG_NEW" >> "$LOG_FILE"
      echo -e "$LOG_NEW" | sudo -u ncuser mutt -s "$SUBJECT" "$TO" -a $LOG_FILE
### Shutdown command taken from:
# http://chris.cnie.de/raspberrypi/cubietruck.html
      sudo shutdown -P -h +0
#       sudo poweroff -h
      exit
   fi
else
### AC adapter plugged in => exit, no battery support required
   exit
fi

It runs fine except that it does not shut down. Below the set value of 10 % battery capacity it keeps on sending an email every 15 minutes until the battery is sucked empty.

 

However, logged in using SSH and initiating one of the following commands will execute as expected:

sudo shutdown -P -h +0
sudo poweroff -h

I searched the forum and other sources on the internet and found some hints to kernels that behaved similarly in the past but I am still not able to find a suitable solution.

 

What am I doing wrong? How did others implemented this use scenario? I would be greatful if somebody points me into the right direction.

 

Thanks a lot in advance.

Cheers,

George

Posted

does your cronjob have root permission? e.g. sudo crontab -e vs. crontab -e...

 

11 minutes ago, Fahrinurlaub said:

It runs fine except that it does not shut down. Below the set value of 10 % battery capacity it keeps on sending an email every 15 minutes until the battery is sucked empty.

means that you get data from:

11 minutes ago, Fahrinurlaub said:

cat /sys/class/power_supply/axp20x-ac/present

don't have axp20x based boards here to test.. :) 

Posted

Have you created a sudoers.d entry for the user calling the sudo command?

 

without a nopass entry for sudo then the sudo initiated commands will never actually run, they will be denied permission to excecute or the script will be sat waiting for a password

 

without that you will need to run the task from root’s crontab not the user crontab like Chew says

Posted

Also try to solve this problem. I create a script that will try to send Warning Email and and shutdown the system as soon as it goes below 10%.

I put it in cron hourly and script should automatically check and do loop if battery discharging and below e.g. 80%.

E-Mail is needed because last time I did not know that my power supply died and I did not know why sever stopped. This version is also producing logs that could be checked after cubietruck fails. That helps for trouble shooting, e.g. to find out that power supply could not produce enough current to charge battery.

Log output example:

Tue Oct 16 10:01:03 CEST 2018 - Ok. Battery Full - 100% left. AC is online with current 0.222 A. Battery health is Good.

 

Posted (edited)
On 9/12/2018 at 12:49 AM, chwe said:

cat /sys/class/power_supply/axp20x-ac/present

And after update to latest Armbian I noticed that it is not there. Update was done from Ubuntu 16.04... And before I can get AC Power supply status via:

cat /sys/class/power_supply/ac/present

Now I could not find anything AC Power supply related... Where is all this gone?

ls -la /sys/class/power_supply
total 0
drwxr-xr-x  2 root root 0 Aug 20 10:26 .
drwxr-xr-x 61 root root 0 Aug 20 10:26 ..
lrwxrwxrwx  1 root root 0 Aug 20 10:26 axp20x-battery -> ../../devices/platform/soc@1c00000/1c2ac00.i2c/i2c-1/1-0034/axp20x-battery-power-supply/power_supply/axp20x-battery
lrwxrwxrwx  1 root root 0 Aug 20 10:26 axp20x-usb -> ../../devices/platform/soc@1c00000/1c2ac00.i2c/i2c-1/1-0034/axp20x-usb-power-supply/power_supply/axp20x-usb

 

UPDATE:

I found it, thanks to this tread:

 

ls -la  /sys/power/axp_pmu/ac
total 0
drwxr-xr-x  2 root root    0 Aug 20 10:33 .
drwxr-xr-x 17 root root    0 Aug 20 10:33 ..
-r--r--r--  1 root root 4096 Aug 20 10:43 amperage
-r--r--r--  1 root root 4096 Aug 20 10:43 connected
-r--r--r--  1 root root 4096 Aug 20 10:43 used
-r--r--r--  1 root root 4096 Aug 20 10:43 voltage

 

Edited by gas_85
RESOLVED
Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

Terms of Use - Privacy Policy - Guidelines