Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[solved] kdialog from bash as root + check battery script
View unanswered posts
View posts from last 24 hours
View posts from last 7 days

 
Reply to topic    Gentoo Forums Forum Index Desktop Environments
View previous topic :: View next topic  
Author Message
musv
Advocate
Advocate


Joined: 01 Dec 2002
Posts: 3364
Location: de

PostPosted: Sun Nov 21, 2010 3:35 pm    Post subject: [solved] kdialog from bash as root + check battery script Reply with quote

Hi there,

I was writing a script to check the remaining capacity of my notebook battery. When it's below a threshold that script should popup a messagebox and warn to plug-in the power supply or otherwise the notebook goes into hibernate within the next few minutes.

The script is working so far except the dialog.

Code:
kdialog --sorry "My Warning"

does the thing as user. What I need is, howto do this as root. I would need something like:
Code:
xhost +localhost && DISPLAY=:0.0 kdialog --sorry "blubb"

but that's not working due to xhost and the dbus session.

How can I realize that?


Last edited by musv on Mon Nov 22, 2010 10:15 pm; edited 1 time in total
Back to top
View user's profile Send private message
DaggyStyle
Watchman
Watchman


Joined: 22 Mar 2006
Posts: 5929

PostPosted: Sun Nov 21, 2010 4:08 pm    Post subject: Reply with quote

how about using sudo into the active user?
_________________
Only two things are infinite, the universe and human stupidity and I'm not sure about the former - Albert Einstein
Back to top
View user's profile Send private message
musv
Advocate
Advocate


Joined: 01 Dec 2002
Posts: 3364
Location: de

PostPosted: Sun Nov 21, 2010 5:41 pm    Post subject: Reply with quote

Thanx, that's working generally. Problem: How to find out the active user?
Back to top
View user's profile Send private message
donmartio
Apprentice
Apprentice


Joined: 11 Dec 2004
Posts: 261

PostPosted: Sun Nov 21, 2010 6:02 pm    Post subject: Reply with quote

Well i would start with something like this:

for name in $(users); do if [ $name != 'root' ]; then sudo -u $name kdialog --sorry "My Warning"; fi; done

Problem here is, if you have opened a few terminals with the same user you have unify the output of users.
Otherwise you will get a more warnings then you wish to.
_________________
Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live.
Back to top
View user's profile Send private message
musv
Advocate
Advocate


Joined: 01 Dec 2002
Posts: 3364
Location: de

PostPosted: Mon Nov 22, 2010 10:13 pm    Post subject: Reply with quote

Ok, I got it. Here's the solution if someone need something similar.

Code:

#!/bin/bash
LOCK_FILE="/tmp/.ac_battery_shutdown_lock"
BAT_STATE_FILE="/proc/acpi/battery/BAT0/state"

ACTION_SHUTDOWN="hibernate"
#ACTION_SHUTDOWN="shutdown -h now"

is_discharge()
{
        return `grep "discharging" "$BAT_STATE_FILE" | wc -l`
}


get_remain_charge()
{
        local __CAPACITY=`sed -n 's/remaining[^0-9]*\([0-9]\+\).*/\1/p' $BAT_STATE_FILE`
        if [ -z "$__CAPACITY" ] ; then
                logger "ACPI_BATTERY_SHUTDOWN: Can't read remaining battery capacity."
        else
                echo $__CAPACITY
        fi
}

get_user()
{
        echo `lsof /usr/bin/dbus-launch | sed -n 's/.*\s\(.*\)\stxt.*/\1/p'`
}

# main program
if  [ -e "$BAT_STATE_FILE" ] ; then

        is_discharge
        if [ "$?" == 1 ] ; then

                CAPACITY=$(get_remain_charge)

                if [[ $CAPACITY -lt 1000 ]] ; then
                        logger "ACPI_BATTERY_SHUTDOWN: Warning, Battery has been reached a value below 1000 mWh (=empty), proceed shutdown."
                        exec $ACTION_SHUTDOWN
                else
                        if [[ $CAPACITY -lt 2000 ]] ; then                     
                                if [ ! -e "$LOCK_FILE" ] ; then
                                        logger "ACPI_BATTERY_SHUTDOWN: Warning, Battery has been reached a value below 2000 mWh."

                                        EUSER=$(get_user)
                                        if [ -n $EUSER ] ; then
                                                DISPLAY=:0.0 sudo -u $EUSER kdialog --sorry "If you don't plug-in the power supply, I'll shut down that thing within the next 4 minutes."
                                        fi
                                        touch $LOCK_FILE
                                fi
                        else   
                                rm -f $LOCK_FILE
                        fi
                fi
        fi
else   
        logger "ACPI_BATTERY_SHUTDOWN: Error, can't find out battery status.  Status file: $BAT_STATE_FILE doesn't exist."
fi


Cronjob:
Code:

*/4 * * * *     /usr/local/sbin/acpi_battery_shutdown


explanation:
  • The script is executed every 4 minutes via cron.
  • First we check, if we are in battery mode and if the battery is discharging (battery state file)
  • Then we read the remaining capacity.
  • if it's below 2000 mWh and no lock-file exists -> create lock-file and pop-up a warning via kdialog. The logged in user you can get via dbus-launch. I guess it's due to the login in kdm.
  • if it's below 1000 mWh -> shutdown or hibernate


After a first test it's working as it should be. Improvements or corrections are welcome.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Desktop Environments All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum