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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
1 1