Linux - Laptop and NetbookHaving a problem installing or configuring Linux on your laptop? Need help running Linux on your netbook? This forum is for you. This forum is for any topics relating to Linux and either traditional laptops or netbooks (such as the Asus EEE PC, Everex CloudBook or MSI Wind).
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
OK, you should have all the pieces, so let's take them one at a time and build you a working application - or figure out where it is broken...
First, test your sudo permissions - let's make sure that actually works (I do not recall testing that).
So you should have in your /etc/sudoers setup (with your user name of course...):
Code:
username ALL=NOPASSWD:/sbin/shutdown -h now
So, let's test it - from a terminal type...
Code:
sudo /sbin/shutdown -h now
Does it work?
Next, let's double check your GUI battery dialog setup.
As noted in earlier posts, select the option to run command NOT in a terminal window. From the thread you linked I believe that was "Run command". Then in the command be sure it includes the full working command tested above:
Code:
sudo /sbin/shutdown -h now
Give that a try. If it does not work it might indicate that the desktop features do not run as your user or root, in which case you would need to consult the docs.
Next, let's make it work from the cron script. You indicated that the data logging cron job was actually running every minute, so I will not include discussion of setting up the crontab here.
So, what we need to do is test whether we are getting an integer number for comparison from your upower command.
I would suggest making a separate short script to test the sed and grep expressions, like this (I'll call it test.sh):
Code:
#!/bin/sh -
PCT=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "percentage" | sed 's/^[^0-9]*\([0-9]*\).*/\1/')
echo "The value of PCT is "${PCT}
Make it executable - chmod +x test.sh
That should give you the integer part of the percentage value returned by upower. It is important that it be an integer and that it not be NULL.
Finally, once you are getting the integer percentage value into $PCT, we need to verify the shutdown command within the real cron script. Lookiing back at earlier posts I think I was sloppy about giving the exact command, so let's make sure we get it right. It should use the exact working upower line from our test script above, so if that is different than shown here be sure to change it, then...
Code:
...
## Add to bottom of your cronbatt.sh
PCT=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "percentage" | sed 's/^[^0-9]*\([0-9]*\).*/\1/')
if [ "$PCT" ] && [ "$PCT" -le "5" ]; then
echo "SHUTTING DOWN AT ${PCT}%" >> /home/XXXX/Scripts/battcron/cronbatt.txt
sudo /sbin/shutdown -h now
fi
For testing, it might be a good idea to change the "5" in the percentage test to something like "80", just so that you can get a live test and plug the power back in if it doesn't work...
But if all these ducks are in a row - it shoulld work! If any of the ducks fails to swim then we can focus on it separately. Good luck!
Last edited by astrogeek; 08-17-2013 at 03:35 PM.
Reason: typos, typos, typos...
OK, you should have all the pieces, so let's take them one at a time and build you a working application - or figure out where it is broken...
First, test your sudo permissions - let's make sure that actually works (I do not recall testing that).
So you should have in your /etc/sudoers setup (with your user name of course...):
Code:
username ALL=NOPASSWD:/sbin/shutdown -h now
So, let's test it - from a terminal type...
Code:
sudo /sbin/shutdown -h now
Does it work?
Next, let's double check your GUI battery dialog setup.
As noted in earlier posts, select the option to run command NOT in a terminal window. From the thread you linked I believe that was "Run command". Then in the command be sure it includes the full working command tested above:
Code:
sudo /sbin/shutdown -h now
Give that a try. If it does not work it might indicate that the desktop features do not run as your user or root, in which case you would need to consult the docs.
Next, let's make it work from the cron script. You indicated that the data logging cron job was actually running every minute, so I will not include discussion of setting up the crontab here.
So, what we need to do is test whether we are getting an integer number for comparison from your upower command.
I would suggest making a separate short script to test the sed and grep expressions, like this (I'll call it test.sh):
Code:
#!/bin/sh -
PCT=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "percentage" | sed 's/^[^0-9]*\([0-9]*\).*/\1/')
echo "The value of PCT is "${PCT}
Make it executable - chmod +x test.sh
That should give you the integer part of the percentage value returned by upower. It is important that it be an integer and that it not be NULL.
Finally, once you are getting the integer percentage value into $PCT, we need to verify the shutdown command within the real cron script. Lookiing back at earlier posts I think I was sloppy about giving the exact command, so let's make sure we get it right. It should use the exact working upower line from our test script above, so if that is different than shown here be sure to change it, then...
Code:
...
## Add to bottom of your cronbatt.sh
PCT=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "percentage" | sed 's/^[^0-9]*\([0-9]*\).*/\1/')
if [ "$PCT" ] && [ "$PCT" -le "5" ]; then
echo "SHUTTING DOWN AT ${PCT}%" >> /home/XXXX/Scripts/battcron/cronbatt.txt
sudo /sbin/shutdown -h now
fi
For testing, it might be a good idea to change the "5" in the percentage test to something like "80", just so that you can get a live test and plug the power back in if it doesn't work...
But if all these ducks are in a row - it shoulld work! If any of the ducks fails to swim then we can focus on it separately. Good luck!
The battery monitor command worked perfectly. Maybe it didnt work because I put in "sudo init 0" instead of "shutdown -h" as well as the location of the shutdown scripts.
I did init 0 because I assumed its a tidier shutdown than shutdown -h ? Regardless, your first comment worked perfectly. Should have tried that to be honest, but it didn't occur to me that the system might treat them differently.
Anyways, issue resolved. Thanks so much for your time and patience.
EDIT: I will test the cron script too, just for mine (and others) future reference.
Update: The cron script still doesnt work, but for all intents and purposes the purpose has been fulfilled so I'm marking this thread as solved anyway.
Ill post the details regardless:
Code:
XXXX@XXXX-Latitude-D430 ~ $ upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "percentage"
percentage: 5.07812%
XXXX@XXXX-Latitude-D430 ~ $ cat test.sh
#!/bin/sh -
PCT=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "percentage" | sed 's/^[^0-9]*\([0-9]*\).*/\1/')
echo "The value of PCT is "${PCT}
XXXX@XXXX-Latitude-D430 ~ $ ./test.sh
The value of PCT is 5
XXXX@XXXX-Latitude-D430 ~ $
So, the value isnt null, thats good....
Code:
XXXX@XXXX-Latitude-D430 ~ $ cat /home/XXXX/Scripts/battcron/cronbatt.sh
#!/bin/sh
ibam --percentbattery >> /home/XXXX/Scripts/battcron/cronbatt.txt
date >> /home/XXXX/Scripts/battcron/cronbatt.txt
uptime >> /home/XXXX/Scripts/battcron/cronbatt.txt
#Enable the following line to add a blank line between records
echo " " >>/home/XXXX/Scripts/battcron/cronbatt.txt
## Add to bottom of your cronbatt.sh
PCT=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "percentage" | sed 's/^[^0-9]*\([0-9]*\).*/\1/')
if [ "$PCT" ] && [ "$PCT" -le "5" ]; then
echo "SHUTTING DOWN AT ${PCT}%" >> /home/XXXX/Scripts/battcron/cronbatt.txt
sudo /sbin/shutdown -h now
fi
Like I said, I did let my battery discharge to below 5% but for whatever reason, the script didnt resolve. Im also sure it was below 5% for more than a minute, so the cron script did have time to react to it.
Last edited by steak1987; 08-19-2013 at 09:42 AM.
Reason: cron scripts
The battery monitor command worked perfectly. Maybe it didnt work because I put in "sudo init 0" instead of "shutdown -h" as well as the location of the shutdown scripts.
I did init 0 because I assumed its a tidier shutdown than shutdown -h ? Regardless, your first comment worked perfectly. Should have tried that to be honest, but it didn't occur to me that the system might treat them differently.
Anyways, issue resolved. Thanks so much for your time and patience.
Great!
Yes, for sudo NOPASSWD commands, they must be used exactly as entered in /etc/sudoers, although you can use wildcards to cover variations. So init 0 is not recognized as shutdown -h now. (Good to see a fellow init'er, I thought I was the last!).
We can continue to figure out why the shutdown does not work from cron if you like - nothing obvious jumps out of the code you pasted, but probably something easy.
Yes, for sudo NOPASSWD commands, they must be used exactly as entered in /etc/sudoers, although you can use wildcards to cover variations. So init 0 is not recognized as shutdown -h now. (Good to see a fellow init'er, I thought I was the last!).
We can continue to figure out why the shutdown does not work from cron if you like - nothing obvious jumps out of the code you pasted, but probably something easy.
Anyway - glad that worked and good luck!
Could we troubleshoot why cron doesnt work ? The method with battery monitor would only work for certain desktop environments, I assume cron is more integral and thus more distro independent. What would I need to start troubleshooting, apart from the information provided above ?
Could we troubleshoot why cron doesnt work ? The method with battery monitor would only work for certain desktop environments, I assume cron is more integral and thus more distro independent. What would I need to start troubleshooting, apart from the information provided above ?
Sure! Be glad to help.
Just to confirm, does the logging part of the cron script actually work - you see the entries every minute (or how often your crontab entry runs)?
And this is the latest code we had for that, is this correct?
Code:
#!/bin/sh
ibam --percentbattery >> /home/XXXX/Scripts/battcron/cronbatt.txt
date >> /home/XXXX/Scripts/battcron/cronbatt.txt
uptime >> /home/XXXX/Scripts/battcron/cronbatt.txt
#Enable the following line to add a blank line between records
echo " " >>/home/XXXX/Scripts/battcron/cronbatt.txt
## Add to bottom of your cronbatt.sh
PCT=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "percentage" | sed 's/^[^0-9]*\([0-9]*\).*/\1/')
if [ "$PCT" ] && [ "$PCT" -le "5" ]; then
echo "SHUTTING DOWN AT ${PCT}%" >> /home/XXXX/Scripts/battcron/cronbatt.txt
sudo /sbin/shutdown -h now
fi
Just to confirm, does the logging part of the cron script actually work - you see the entries every minute (or how often your crontab entry runs)?
And this is the latest code we had for that, is this correct?
Code:
#!/bin/sh
ibam --percentbattery >> /home/XXXX/Scripts/battcron/cronbatt.txt
date >> /home/XXXX/Scripts/battcron/cronbatt.txt
uptime >> /home/XXXX/Scripts/battcron/cronbatt.txt
#Enable the following line to add a blank line between records
echo " " >>/home/XXXX/Scripts/battcron/cronbatt.txt
## Add to bottom of your cronbatt.sh
PCT=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "percentage" | sed 's/^[^0-9]*\([0-9]*\).*/\1/')
if [ "$PCT" ] && [ "$PCT" -le "5" ]; then
echo "SHUTTING DOWN AT ${PCT}%" >> /home/XXXX/Scripts/battcron/cronbatt.txt
sudo /sbin/shutdown -h now
fi
Yes, that is my current script^
Thats weird. The I opened the text (cronbatt.txt) file, it wasnt being updated. I commented out the cron script, and emptied the document (maybe it was becoming too big), uncommented the cron script, but, nah, it isnt writing anything.
Also strange in ./ not being able to run my script but sh runs it fine. Oh well...
Code:
XXXX@XXXX-Latitude-D430 ~/Scripts/battcron $ sudo ./cronbatt.sh
sudo: ./cronbatt.sh: command not found
XXXX@XXXX-Latitude-D430 ~/Scripts/battcron $ sh cronbatt.sh
XXXX@XXXX-Latitude-D430 ~/Scripts/battcron $ cat cronbatt.sh
#!/bin/sh
ibam --percentbattery >> /home/XXXX/Scripts/battcron/cronbatt.txt
date >> /home/XXXX/Scripts/battcron/cronbatt.txt
uptime >> /home/XXXX/Scripts/battcron/cronbatt.txt
#Enable the following line to add a blank line between records
echo " " >>/home/XXXX/Scripts/battcron/cronbatt.txt
## Add to bottom of your cronbatt.sh
PCT=$(upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "percentage" | sed 's/^[^0-9]*\([0-9]*\).*/\1/')
if [ "$PCT" ] && [ "$PCT" -le "5" ]; then
echo "SHUTTING DOWN AT ${PCT}%" >> /home/XXXX/Scripts/battcron/cronbatt.txt
sudo /sbin/shutdown -h now
fi
XXXX@XXXX-Latitude-D430 ~/Scripts/battcron $
My crontab is as follows:
Code:
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
* * * * * /home/XXXX/Scripts/battcron/cronbatt.sh
Thats weird. The script seems to work when I "sh" it, and shows the output in the text file, but otherwise, there is no logging going on.
Code:
Battery percentage: 25 %
Battery time left: 0:25:45
Adapted battery time left: 0:25:45
Sun Aug 25 14:12:47 EST 2013
14:12:47 up 7 min, 2 users, load average: 0.02, 0.21, 0.16
Battery percentage: 23 %
Battery time left: 0:23:59
Adapted battery time left: 0:48:41
Sun Aug 25 14:18:43 EST 2013
14:18:43 up 13 min, 2 users, load average: 0.27, 0.26, 0.22
Battery percentage: 23 %
Battery time left: 0:23:59
Adapted battery time left: 0:48:41
Sun Aug 25 14:18:53 EST 2013
14:18:53 up 13 min, 2 users, load average: 0.23, 0.25, 0.21
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.