I have written a two small scripts to help me auto-shutdown my laptop at a certain battery percentage. Like the previous post I have not used crunchbag.
I have an old laptop with an old battery. With Ubuntu 10.10 there is an auto-shutdown program, but once its starts the battery runs out before it properly shuts down. As well it was annoying when I plugged in a power source but the computer shuts down anyway.
I liked the idea the proposed scripts above, but wanted more out of them. So I decided to write my own and tweak theirs.
This is the first time a write scripts like this so: -1: be nice -2: please post any improvements you have.
Here it goes (I suggest that you save and create all of these files in the same place):
The first thing to do was to write a script with a popup window with a warning and a countdown until shut down.
I chose a progress bar in zenity:
#!/bin/sh
sudo shutdown -t 60 -h +1 &
(
echo "1" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "2" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "3" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "4" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "5" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "6" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "7" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "8" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "9" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "10" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "11" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "12" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "13" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "14" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "15" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "16" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "17" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "18" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "19" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "20" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "21" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "22" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "23" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "24" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "25" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "26" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "27" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "28" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "29" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "30" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "31" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "32" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "33" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "34" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "35" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "36" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "37" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "38" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "39" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "40" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "41" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "42" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "43" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "44" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "45" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "46" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "47" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "48" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "49" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "50" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "51" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "52" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "53" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "54" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "55" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "56" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "57" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "58" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "59" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "60" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "61" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "62" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "63" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "64" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "65" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "66" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "67" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "68" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "69" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "70" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "71" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "72" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "73" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "74" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "75" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "76" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "77" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "78" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "79" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "80" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "81" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "82" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "83" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "84" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "85" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "86" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "87" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "88" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "89" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "90" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "91" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "92" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "93" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "94" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "95" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "96" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "97" ; sleep 0.3
echo "# Low Battery: Plug Power Source" ; sleep 0.3
echo "98" ; sleep 0.3
echo "# Shutting Down now..." ; sleep 0.3
echo "99" ; sleep 0.3
echo "# Shutting Down now..." ; sleep 0.3
echo "100" ; sleep 0.3
) | if zenity --progress --width 350 --progress --title="Low Battery" --text="Doing important things..." --percentage=100
then
zenity --error --text="Ok, Ok, I'm shutting down now!"
else
sudo shutdown -c
exit
fi
As you can see the progress bar is for aesthetics only, but the bar and the countdown are coordinated. The timer is one minute. I don't know how to have it less than that.
Then it was important to me that this countdown was stopped if I managed to plug in a power source fast enough. So I wrote the second script. (by the way I hope you are not running zenity programs on your laptop or this script will kill them):
#!/bin/bash
until grep -q "1" "/sys/class/power_supply/ACAD/online"
do
{ if grep -q "1" "/sys/class/power_supply/ACAD/online"
then
sudo shutdown -c & sudo killall conkyfinal.
sudo killall zenity
fi }
done
You need to give these scripts permission to run as programs. So right click on the scripts, choose properties, permission tab and tick the allow to run as program box. As well you will probably notice that you need root access to shutdown your laptop. This means that you need to give these files root access.
www.tuxation.com/setuid-on-shell-scripts.html
What you want to do is open your favorite text editor and paste this:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
setuid( 0 );
system( "/path/to/script.sh" );
return 0;
}
Save it as runscript.c (or any other name)
Now open a terminal and find your way to the file you saved runscript.c with the other files. And execute these commands:
gcc runscript.c -o runscript
sudo chown root:root runscript
sudo chmod 4755 runscript
Now remember that you need to do this for each script. Just change the runcript.c to runscript2.c and the rest of the commands as well. Remember to change the path name to the second file.
Now that we have the scripts we need to make conky execute them when we want. Paste this at the end of your conky script. (normally gedit ~/.conkyrc)
${if_existing /sys/class/power_supply/ACAD/online 0}
${if_match ${battery_percent BAT1} <= 20}${color2}${exec /path/to/runscript}${endif}${endif}
Change the values of BAT1 to BAT0 or any other according to your laptops battery. To find out execute this in your terminal and the output should be your battery name:
I have chosen 20% of battery because my laptops battery is that bad.
Now I came across a problem at that point because I could not execute two scripts at the same time in conky. So we need to create another conky to be executed on start up with, to execute the second script.
First we need to create a new conky file with to execute the second script. Mine displays a single sentence on the far left corner to make sure that that conky is actually working. It looks like this:
######################
# - Conky settings - #
######################
update_interval 1
total_run_times 0
net_avg_samples 1
cpu_avg_samples 1
imlib_cache_size 0
double_buffer yes
no_buffers yes
format_human_readable
#####################
# - Text settings - #
#####################
use_xft yes
xftfont Droid Sans:size=9
override_utf8_locale yes
text_buffer_size 2048
#############################
# - Window specifications - #
#############################
own_window_class Conky
own_window yes
own_window_type override
own_window_transparent yes
own_window_hints undecorated,below,sticky,skip_taskbar,skip_pager
alignment top_left
gap_x 7
gap_y 32
minimum_size 182 0
maximum_width 182
default_bar_size 85 5
#########################
# - Graphics settings - #
#########################
draw_shades no
default_color cccccc
color0 white
color1 white
color2 white
color3 2B893E
color4 A05A06
color5 A09A06
TEXT
${font Droid Sans:style=Bold:size=9}StopShut activated ${font}
${if_existing /sys/class/power_supply/ACAD/online 0}
${if_match ${battery_percent BAT1} <= 21}${color2}${exec /home/melclic1988/Documents/mybin/runscript2}${endif}${endif}
But the important lines are:
${if_existing /sys/class/power_supply/ACAD/online 0}
${if_match ${battery_percent BAT1} <= 21}${color2}${exec /path/to/runscript2}${endif}${endif}
It is important that this script lunches before the other one. So for the battery percentage put the percentage slightly higher for this script compared to the other one.
Now you need to change your start up application script so that both these conky files are executed at the startup of your laptop. So change the start up conky file to this one:
#!/bin/bash
sleep 15 &&
conky -d -c /path/to/firstscript &
sleep 15 &&
conky -d -c /path/to/secondscriptt &
exit
Remember I am not responsible if these scripts do not work. And don't forget that making any file have auto-root access may be dangerous for your system.
Have fun.
Last edited by melclic1988 (2010-11-07 16:21:25)