View previous topic :: View next topic |
Author |
Message |
musv Advocate
Joined: 01 Dec 2002 Posts: 3365 Location: de
|
Posted: Sun Nov 21, 2010 3:35 pm Post subject: [solved] kdialog from bash as root + check battery script |
|
|
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 |
|
|
DaggyStyle Watchman
Joined: 22 Mar 2006 Posts: 5929
|
Posted: Sun Nov 21, 2010 4:08 pm Post subject: |
|
|
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 |
|
|
musv Advocate
Joined: 01 Dec 2002 Posts: 3365 Location: de
|
Posted: Sun Nov 21, 2010 5:41 pm Post subject: |
|
|
Thanx, that's working generally. Problem: How to find out the active user? |
|
Back to top |
|
|
donmartio Apprentice
Joined: 11 Dec 2004 Posts: 261
|
Posted: Sun Nov 21, 2010 6:02 pm Post subject: |
|
|
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 |
|
|
musv Advocate
Joined: 01 Dec 2002 Posts: 3365 Location: de
|
Posted: Mon Nov 22, 2010 10:13 pm Post subject: |
|
|
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 |
|
|
|