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.
Im trying to see how well my battery runs and lasts in real life scenarios, but I cant seem to find any applications or tools or scripts in linux. I can understand that one option is loading a long (10h) youtube video or a long video playlist of its offline, but I cant leave them running, I need to be there to time when the power finally goes.
Ive got powertop and I tried looking at the help and cooking up a command:
Code:
XXXX@XXXX-Latitude-D430 ~ $ powertop --help
Usage: powertop [OPTIONS]
--debug run in "debug" mode
--version print version information
--calibrate runs powertop in calibration mode
--extech[=devnode] uses an Extech Power Analyser for measurements
--html[=FILENAME] generate a html report
--csv[=FILENAME] generate a csv report
--time[=seconds] generate a report for 'x' seconds
--iteration[=iterations] number of times to run each test
--workload[=workload] file to execute for workload
--quite supress stderr output
--help print this help menu
For more help please refer to the README
XXXX@XXXX-Latitude-D430 ~ $ sudo powertop -html[=/home/XXXX/batt.html] --time[=5]
powertop: unrecognised option '--time[=5]'
XXXX@XXXX-Latitude-D430 ~ $ sudo powertop -html[=/home/XXXX/batt.html]
Loaded 361 prior measurements
Preparing to take measurements
Taking 1 measurement(s) for a duration of 20 second(s) each.
Cannot open output file tml[=/home/XXXX/batt.html (No such file or directory)
PowerTOP outputing using base filename tml[=/home/XXXX/batt.html
XXXX@XXXX-Latitude-D430 ~ $
I have not used powertop, but it happens that within the past few days I wote short cron script to log battery state to a MySQL database, once per minute in my case.
I can then query that data to see fine grained charge and discharge data for as long a period as I like. I have not written any special methods for viewing the data, I just query it and pipe into gnuplot at this time.
If interested I'll post the script and the DB table structure, and tell you how to get it running... then you are on your own.
I was actually thinking of that. Could it be possible to have a small script in the background saving the output of the uptime command as well as another command showing battery life ?
Maybe having the output saved to a txt file.
However, there is one issue that comes to mind. Im running on solid state memory, and im worried about what will happen if I were to be writing the data onto the disk as the power fails ? I would risk memory corruption, wouldnt I ?
In that case, is there any way to make a script that logs the battery percentate and the uptime (or script start time), till maybe 5% battery capacity, at which point it stops recording ?
Looking around google, Ive seen the ibam package is pretty helpful:
Also, how would I schedule this to run every 60 seconds ?
Is there any way to stop recording when the battery percentage runs below a certain amount ?
Thanks for your advise...
Hey! Sorry to be so long responding, have been away the past couple of days.
As for your question quoted here, see man cron, but here is a quick answer:
1. Create a shell script that captures and records the battery data you want to keep, let's call it batterycapture.sh
2. Make sure the script is executable, like this: chmod +x batterycapture.sh (and test it to be sure it does what you want!)
2. Set up a line in your crontab that runs this script every minute of every day using crontab -e, like this...
At this point you will be collecting raw battery data and can query it any way that you like.
You may want to see other things as you indicated in your earlier posts, so you will need to modify the table structure to store them and modify the script to capture them, but let's consider that beyond the scope of this particular post and focus on getting the database working and make sure the basic data I capture is available on your system.
One additional point - if you leave the cron running forever you will get 1440 new data points in the DB every day. That is not a problem, but might not be really necessary. For my own use I enable the cron then pull the power cord and let the battery power my laptop until it is nearly depleted during normal use. I then repeat for the recharge cycle, and disable the cron afterward. That gives me single complete data set.
I hope this is not too cryptic and is helpful to you!
Hey! Sorry to be so long responding, have been away the past couple of days.
As for your question quoted here, see man cron, but here is a quick answer:
1. Create a shell script that captures and records the battery data you want to keep, let's call it batterycapture.sh
2. Make sure the script is executable, like this: chmod +x batterycapture.sh (and test it to be sure it does what you want!)
2. Set up a line in your crontab that runs this script every minute of every day using crontab -e, like this...
It will then run your script every minute of every day of every week of every month.
To turn it off simply remove or comment out the line in your crontab.
I'll follow shortly with another post about how I do it with MySQL.
I really have very little experience with MySQL so ill probably use the script+crontab method.
I want to write the output of two different commands to the same text file, after each other.
I tried using google and came up with a way to display all the information in the terminal window:
Code:
$ ibam --percentbattery; uptime
Battery percentage: 83 %
Battery time left: 2:14:16
Adapted battery time left: 2:55:00
11:58:06 up 30 min, 2 users, load average: 0.06, 0.45, 0.52
The question now is, how would I save the output to a text file ? I tried this command:
I really have very little experience with MySQL so ill probably use the script+crontab method.
That is understandable, I did not mean to push it on you, but offered as "the way I do it".
Quote:
Originally Posted by steak1987
I want to write the output of two different commands to the same text file, after each other.
I tried using google and came up with a way to display all the information in the terminal window:
...
The question now is, how would I save the output to a text file ? I tried this command:
however, only the uptime and the load is recorded, not the battery statistics.
Yea, only the second is going to the file. You could do it all on one line, but if you are adding it to the cron anyway, it is probably better to put it into a file.
I would put the following in a file, call it cronbatt.sh, then add that file to your crontab:
Code:
#!/bin/sh
ibam --percentbattery >> /path_to/data_file
uptime >> /path_to/data_file
#Enable the following line to add a blank line between records
echo " " >>/path_to/data_file
Then
Code:
chmod +x cronbatt.sh
Then add to your crontab...
Code:
* * * * * /path_to/cronbatt.sh
In both the cronbatt.sh script and the crontab line, the paths need to be full path from /, so I have shown the leading slash to emphasize that.
Hope this helps!
Last edited by astrogeek; 08-11-2013 at 03:03 PM.
Reason: typos
#!/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
as for the crontab file:
Code:
GNU nano 2.2.6 File: /tmp/crontab.eqsEAF/crontab
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# 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
Works beautifully.
One last question though, in order to prevent damage with the battery failing as the battery file is being written, I want to have the computer go through a "tidy" shutdown when the battery is at a really low percent, like 5-7%.
But, I'm worried about modifying access rights to folders in my home folder. Any tips ? Can I just modify the access rights to the file without touching the folder rights ?
Not sure where you are saying you want to put the shutdown command.
I do not recognize the dialog in the image you attached, but if it is a system configuration dialog then it should have the necessary rights to run init 0 or shutdown. If it is a KDE or Gnome desktop dialog then maybe or maybe not. The best I can do for those is tell you to see the available docs as I do not use either.
On the other hand, if you want to detect the level and do the shutdown from the cron script, then I would suggest moving the script to a root owned location, maybe /sbin or somewhere in /root/... and add it to the root crontab instead of the normal user - then permissions are not a problem.
You could change ownership of just the cronbatt.sh script to root, but if it is running from your user crontab it will still be running with your user permission, not root.
Yea, that probably runs as the login user, and yes, that forum thread looks like exactly what you need!
Quote:
Originally Posted by steak1987
Should I leave username unchanged or use my own ?
EDIT: also, can I use "gksu gedit" as opposed to visudo ?
Use your own user name in that line and add it to /etc/sudoers.
Not sure about gksu as I do not have it on Slackware (my distro of choice). Looking online it looks like a user front end to sudo, not a replacement for visudo. On the other hand, if it will allow you to edit /etc/sudoers then it will work. If not, just open a terminal, be root and visudo.
Using that, I figured out how to use grep (YAY!), and the command I am supposed to do is:
Code:
upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "state|to\ full|percentage"
state: charging
time to full: 51.3 minutes
percentage: 28.0971%
My idea is to make a script using that command, and run a cron job every minute, to see if the battery percentage drops below 5% (without a charger present), then to initiate shutdown.
so, I made the bash script, and chmoded it:
Code:
XXXX@XXXX-Latitude-D430 ~/Scripts/battshutdown $ sudo chmod +x battshutdown.sh
[sudo] password for XXXX:
XXXX@XXXX-Latitude-D430 ~/Scripts/battshutdown $ ./ battshutdown.sh
bash: ./: Is a directory
XXXX@XXXX-Latitude-D430 ~/Scripts/battshutdown $ ./battshutdown.sh
state: charging
time to full: 51.6 minutes
percentage: 35.8538%
XXXX@XXXX-Latitude-D430 ~/Scripts/battshutdown $ cat battshutdown.sh
#!/bin/sh
upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "state|to\ full|percentage"
So far, its all been relatively simple, but how would I get a script to read the output of that command, and if the value of "percentage" is less than 5, and the "state" is discharging, then run a shutdown script (sudo init 0) ?
Apologies if the answer seems pretty obvious, I cant seem to figure out what to do.
Ooookay, I thought it was smooth sailing, apparently not.
Code:
#includedir /etc/sudoers.d
#username ALL=NOPASSWD:/sbin/shutdown -h now
XXXX ALL=NOPASSWD:/sbin/shutdown -h now
Unfortunately it doesnt seem to work, but this may be with the battery monitor widget than sudoers.
Actually, that should work as long as the battery monitor thingy actually runs as your user name.
Looking back at the referenced thread, there is apparently a selection for "Run in terminal" or "Run command" (I forget exact wording). Be sure to select NOT run in terminal from that applet.
Using the applet would make the shutdown independent of whether your script was running - probably a good thing, so I'd check that first.
Quote:
Originally Posted by steak1987
Alright, new option maybe ? Something possibly more robust ?
Using that, I figured out how to use grep (YAY!), and the command I am supposed to do is:
Code:
upower -i /org/freedesktop/UPower/devices/battery_BAT0 | grep -E "state|to\ full|percentage"
state: charging
time to full: 51.3 minutes
percentage: 28.0971%
My idea is to make a script using that command, and run a cron job every minute, to see if the battery percentage drops below 5% (without a charger present), then to initiate shutdown.
[SNIP]
So far, its all been relatively simple, but how would I get a script to read the output of that command, and if the value of "percentage" is less than 5, and the "state" is discharging, then run a shutdown script (sudo init 0) ?
Apologies if the answer seems pretty obvious, I cant seem to figure out what to do.
You could do that. But since your battery log cron is already running every minute, why not just include it in there.
Something like this should do it, and write the shutdown to the log in the process...
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
shutdown -h now
fi
You could of course run that as a separate cron, but if your cronbatt.sh is always running anyway, why not combine them?
Note that I tested the sed expression on a string, but I do not have upower on my system either, so be sure to test it before letting it run free! Maybe disable the shutdown line and echo the output manually to be sure it works with the upower output.
Hope this helps! Let me know!
[EDIT]
Just realized this does not check for charger present, but in practice that should not be a problem. If it is required we can add detection for that too, but out of time for now.
[/EDIT]
Actually, that should work as long as the battery monitor thingy actually runs as your user name.
Looking back at the referenced thread, there is apparently a selection for "Run in terminal" or "Run command" (I forget exact wording). Be sure to select NOT run in terminal from that applet.
Using the applet would make the shutdown independent of whether your script was running - probably a good thing, so I'd check that first.
You could do that. But since your battery log cron is already running every minute, why not just include it in there.
Something like this should do it, and write the shutdown to the log in the process...
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
shutdown -h now
fi
You could of course run that as a separate cron, but if your cronbatt.sh is always running anyway, why not combine them?
Note that I tested the sed expression on a string, but I do not have upower on my system either, so be sure to test it before letting it run free! Maybe disable the shutdown line and echo the output manually to be sure it works with the upower output.
Hope this helps! Let me know!
[EDIT]
Just realized this does not check for charger present, but in practice that should not be a problem. If it is required we can add detection for that too, but out of time for now.
[/EDIT]
I tried both options, the first one didnt work, when I selected "run in terminal window", a terminal popup appeared, waiting for a password (I couldnt get to it in time, the battery died).
EDIT: Waiting to see if your script works, had some issues, will update this thread either way
You could do that. But since your battery log cron is already running every minute, why not just include it in there.
Something like this should do it, and write the shutdown to the log in the process...
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
shutdown -h now
fi
You could of course run that as a separate cron, but if your cronbatt.sh is always running anyway, why not combine them?
Note that I tested the sed expression on a string, but I do not have upower on my system either, so be sure to test it before letting it run free! Maybe disable the shutdown line and echo the output manually to be sure it works with the upower output.
Hope this helps! Let me know!
[EDIT]
Just realized this does not check for charger present, but in practice that should not be a problem. If it is required we can add detection for that too, but out of time for now.
[/EDIT]
I tried running that as a cron job, no luck. I let the battery run down to about 3% (monitored by the following command:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.