LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Laptop and Netbook
User Name
Password
Linux - Laptop and Netbook Having 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



Reply
 
Search this Thread
Old 08-17-2013, 04:30 PM   #16
astrogeek
Senior Member
 
Registered: Oct 2008
Distribution: Slackware [64]X{.0|.1|.2|-current} ::X>=12<=14, FreeBSD_10{.0|.1}
Posts: 2,172

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855

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 04:35 PM. Reason: typos, typos, typos...
 
1 members found this post helpful.
Old 08-19-2013, 10:11 AM   #17
steak1987
Member
 
Registered: Jan 2012
Posts: 128

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by astrogeek View Post
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 10:42 AM. Reason: cron scripts
 
Old 08-19-2013, 04:15 PM   #18
astrogeek
Senior Member
 
Registered: Oct 2008
Distribution: Slackware [64]X{.0|.1|.2|-current} ::X>=12<=14, FreeBSD_10{.0|.1}
Posts: 2,172

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
Quote:
Originally Posted by steak1987 View Post
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.

Anyway - glad that worked and good luck!
 
Old 08-22-2013, 01:09 AM   #19
steak1987
Member
 
Registered: Jan 2012
Posts: 128

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by astrogeek View Post
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.

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 ?
 
Old 08-22-2013, 01:27 AM   #20
astrogeek
Senior Member
 
Registered: Oct 2008
Distribution: Slackware [64]X{.0|.1|.2|-current} ::X>=12<=14, FreeBSD_10{.0|.1}
Posts: 2,172

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
Quote:
Originally Posted by steak1987 View Post
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
 
1 members found this post helpful.
Old 08-25-2013, 12:20 AM   #21
steak1987
Member
 
Registered: Jan 2012
Posts: 128

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by astrogeek View Post
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
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
Something must have broken on the way ...

Last edited by steak1987; 08-25-2013 at 12:22 AM.
 
Old 08-25-2013, 12:47 AM   #22
astrogeek
Senior Member
 
Registered: Oct 2008
Distribution: Slackware [64]X{.0|.1|.2|-current} ::X>=12<=14, FreeBSD_10{.0|.1}
Posts: 2,172

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
Quote:
Originally Posted by steak1987 View Post
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.
Welcome back!

Sounds like it is not executable. What does ls -l cronbatt.sh say?
 
1 members found this post helpful.
Old 08-27-2013, 05:05 AM   #23
steak1987
Member
 
Registered: Jan 2012
Posts: 128

Original Poster
Rep: Reputation: Disabled
Code:
XXXX@XXXX-Latitude-D430 ~/Scripts/battcron $  ls -l cronbatt.sh 
-rw-r--r-- 1 XXXX XXXX 615 Aug 25 12:38 cronbatt.sh


_________________________________________
EDIT: I tried this out:

http://www.linfo.org/create_shell_1.html

Code:
XXXX@XXXX-Latitude-D430 ~/Scripts/battcron $ sudo chmod 775 cronbatt.sh 
[sudo] password for XXXX: 
XXXX@XXXX-Latitude-D430 ~/Scripts/battcron $ ./cronbatt.sh 
XXXX@XXXX-Latitude-D430 ~/Scripts/battcron $  ls -l cronbatt.sh 
-rwxrwxr-x 1 XXXX XXXX 615 Aug 25 12:38 cronbatt.sh
And now it works. I thought chmod +x would make it executable, but clearly there were flaws in my understanding.

Anyways, cheers, thanks for pointing me in the right direction. Clearly my understanding chmod +x was limited.

Last edited by steak1987; 08-27-2013 at 05:13 AM. Reason: issue resolved after basic web search
 
Old 08-27-2013, 12:56 PM   #24
astrogeek
Senior Member
 
Registered: Oct 2008
Distribution: Slackware [64]X{.0|.1|.2|-current} ::X>=12<=14, FreeBSD_10{.0|.1}
Posts: 2,172

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
Quote:
Originally Posted by steak1987 View Post
Code:
XXXX@XXXX-Latitude-D430 ~/Scripts/battcron $  ls -l cronbatt.sh 
-rw-r--r-- 1 XXXX XXXX 615 Aug 25 12:38 cronbatt.sh


_________________________________________
EDIT: I tried this out:

http://www.linfo.org/create_shell_1.html

Code:
XXXX@XXXX-Latitude-D430 ~/Scripts/battcron $ sudo chmod 775 cronbatt.sh 
[sudo] password for XXXX: 
XXXX@XXXX-Latitude-D430 ~/Scripts/battcron $ ./cronbatt.sh 
XXXX@XXXX-Latitude-D430 ~/Scripts/battcron $  ls -l cronbatt.sh 
-rwxrwxr-x 1 XXXX XXXX 615 Aug 25 12:38 cronbatt.sh
And now it works. I thought chmod +x would make it executable, but clearly there were flaws in my understanding.

Anyways, cheers, thanks for pointing me in the right direction. Clearly my understanding chmod +x was limited.
Hmmm... chmod +x should have done it. Maybe a typo or one of those cosmic glitches got you!

Anyway, glad to hear it is working! Good luck!

Last edited by astrogeek; 08-27-2013 at 11:16 PM. Reason: typos, typos, typos...
 
Old 08-27-2013, 10:27 PM   #25
steak1987
Member
 
Registered: Jan 2012
Posts: 128

Original Poster
Rep: Reputation: Disabled
Cheers, thanks for your time.
 
Old 08-27-2013, 11:16 PM   #26
astrogeek
Senior Member
 
Registered: Oct 2008
Distribution: Slackware [64]X{.0|.1|.2|-current} ::X>=12<=14, FreeBSD_10{.0|.1}
Posts: 2,172

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
You are very welcome!
 
  


Reply

Tags
battery, benchmark, benchmarking


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Benchmark program for Linux? Larry James Linux - General 3 12-04-2008 11:04 AM
battery life benchmark on gnome vs kde paulsiu Linux - Laptop and Netbook 1 05-08-2008 11:46 AM
3D Benchmark in Linux ? houmie Suse/Novell 4 04-06-2005 01:46 PM
All Around Linux Benchmark TuxFreak Linux - Software 2 11-25-2004 06:40 PM
Benchmark linux p4 vs mac 9 or 10 iwkua Linux - General 3 02-22-2004 08:21 AM


All times are GMT -5. The time now is 06:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration