Xfce Forum

Sub domains
 

You are not logged in.

#1 2014-10-11 14:17:36

sonycdr
Member
Registered: 2009-01-30
Posts: 130

[Xfce Power Manager] Feature request to run custom code

Hi guys,

What do you think about the idea to run custom code depending on the AC state?

For example start /usr/bin/dropboxd if laptop is charging. If laptop is running on battery, run /usr/bin/killall dropboxd.

Would you find that a useful addition to Xfce Power Manager?

Thanks for your answers!

Last edited by sonycdr (2014-10-11 14:17:49)

Offline

#2 2014-10-11 15:16:11

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 11,006

Re: [Xfce Power Manager] Feature request to run custom code

That would definitely be a good idea - especially for user-based scripts.

As a workaround, if you are using a distro that uses pm-utils, you can tie this into the power management sub-system. Create a file in your /etc/pm/power.d directory with the following content:

#!/bin/bash
USERNAME=userid
case $1 in
    true) #on_battery 
        /usr/bin/killall dropboxd
        ;;
    false) #on_ac
        export DISPLAY=":0"
        export XAUTHORITY="/home/$USERNAME/.Xauthority"
        su $USERNAME -c "/usr/bin/dropboxd"
	;;
    *) exit 1 ;;
esac

exit 0

...change the "USERNAME=userid" to specify your username and make the file executable. On every change of power status (on_battery vs on_ac), this script will be run and it should stop and start dropbox.

This is also possible if you are using a distro that uses systemd, but I don't know the commands off of the top of my head.


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#3 2014-10-11 16:42:47

sonycdr
Member
Registered: 2009-01-30
Posts: 130

Re: [Xfce Power Manager] Feature request to run custom code

@ToZ

Thanks a lot for your comment! I am glad that you find the idea useful.

I created the directory and the script above but I am not sure if it will work.

I am using Arch Linux and systemd. I tried to install pm-utils but it conflicts with TLP that I am heavily using:

:: pm-utils and tlp are in conflict. Remove tlp? [y/N] 

This is also possible if you are using a distro that uses systemd, but I don't know the commands off of the top of my head.

It would be great to know how to do that with systemd!

Offline

#4 2014-10-12 01:51:11

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 11,006

Re: [Xfce Power Manager] Feature request to run custom code

Then lets just use udev.

1. Create the file /usr/local/bin/dropbox_power as above with the following content:

#!/bin/bash
USERNAME=userid
case $1 in
    true) #on_battery 
        /usr/bin/killall dropbox
        ;;
    false) #on_ac
        export DISPLAY=":0"
        export XAUTHORITY="/home/$USERNAME/.Xauthority"
        su $USERNAME -c "/usr/bin/dropboxd"
	;;
    *) exit 1 ;;
esac

exit 0

...and make the file executable. Make sure to change the "USERNAME=userid" to suit. Also note, that on my system, the dropbox executable that is running is called just dropbox (not dropboxd). I've made that change to this script.

2. Create the udev rule as /etc/udev/rules.d/98-dropbox.rules with the following content:

SUBSYSTEM=="power_supply", ATTR{online}=="0", RUN+="/usr/local/bin/dropbox_power true"

SUBSYSTEM=="power_supply", ATTR{online}=="1", RUN+="/usr/local/bin/dropbox_power false"

...and reload the rules:

sudo udevadm control --reload-rules

3. Test it out.


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#5 2014-10-12 18:30:54

sonycdr
Member
Registered: 2009-01-30
Posts: 130

Re: [Xfce Power Manager] Feature request to run custom code

I was thinking about a Udev solution before but it is not the recommended way to do:

https://bugs.launchpad.net/ubuntu/+sour … ug/1297248

Basically udev should only be used for short calls (max 30s) but not to run full scripts.

Offline

#6 2014-10-12 20:18:44

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 11,006

Re: [Xfce Power Manager] Feature request to run custom code

This script shouldn't take more than a couple of seconds to run - won't be anywhere near 30 seconds. And since udev already recognizes the plug/unplug action, it makes sense to me to tie it in there.


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#7 2014-10-14 08:57:09

sonycdr
Member
Registered: 2009-01-30
Posts: 130

Re: [Xfce Power Manager] Feature request to run custom code

I will try it out, thanks!

Concerning adding a custom code feature to power manager, how should I proceed?

Are the developers are my request already or should I post it on the bugtracker?

Offline

#8 2014-10-14 11:01:42

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 11,006

Re: [Xfce Power Manager] Feature request to run custom code

sonycdr wrote:

Are the developers are my request already or should I post it on the bugtracker?

It would be best to post a request on the bug tracker.


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#9 2014-10-15 06:22:44

sonycdr
Member
Registered: 2009-01-30
Posts: 130

Re: [Xfce Power Manager] Feature request to run custom code

Created a new bug report. Curious to see the comments from the developers.

https://bugzilla.xfce.org/show_bug.cgi?id=11236

Offline

Board footer

Powered by FluxBB