1 1
Fahrinurlaub

Cubietruck power off with battery installed

Recommended Posts

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

Share this post


Link to post
Share on other sites

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.. :) 

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
1 1