Ubuntu disable/enable compiz when on battery/AC bash script

HP Compaq Presario CQ56-100SMLast week I won an award from my college sponsor Končar Electrical Engineering Institute for my graduate work on the topic of creating an advanced web social networking system using open source technologies. I must express my gratitude to the fine people on my college who nominated me and to the Končar Electrical Engineering Institute for rewarding my effort. By the way, this social networking system is work in progress and when ready, I will set it up for testing because I value your feedback for my little projects like Quick Chat.

What did I do with my reward you ask? I bought myself a laptop. My laptop selection priorities were low price and Linux operating system compatibility. I have selected HP Compaq Presario CQ56 with Suse Enterprise Linux11 factory installed. It is really good laptop for the price and very Linux friendly if any of you Linux fans are on a Laptop hunt. I might post a review on this great piece of hardware when I find some available time. Yesterday I've installed Ubuntu Maverick on this laptop and everything works great. But when everything works great, we people usually go to the picky side and start inventing problems. So to create myself some work, I've given myself a task of creating script to turn Compiz off when on battery, and turn it back on when on AC. My goal was extending my shiny new battery life for a few minutes and have fun in the process of creating this script. So here it is...

I'm not gonna get into a details of inner working on this commands, but I will try to explain basic things.

#!/bin/bash
 
export DISPLAY=:0.0
if on_ac_power; then
	su -p -c 'notify-send -u critical -i "notification-message-IM" "On AC: Desktop compositing" "Turning desktop composition on"' username
	sleep 1
	su -p -c '/usr/bin/compiz --replace &' username
else
	su -p -c 'notify-send -u critical -i "notification-message-IM" "On battery: Desktop compositing" "Turning desktop composition off"' username
	sleep 1
	su -p -c '/usr/bin/metacity --replace &' username
fi
 
exit 0

What we need to do now is to put our script in the path for the scripts that are executed every time you plug/unplug you laptop to/from the AC. So lets create this script file and paste contents from the upper code block.

gksudo gedit '/etc/pm/power.d/99_compiz'

Now we paste code from the upper bash code block, replace username with your username and then save our changes. After our script is in place we will mark it as executable using chmod:

sudo chmod +x /etc/pm/power.d/99_compiz

One last but very important thing. Like all other system scripts, this script is launched as root and it controls compz by using su to execute commands as you non root user. We can't control another users X application like Compiz even as root users (this is security messure, a good one if you ask me). So we must allow X connections to localhost using command like xhost +127.0.0.1. If you don't want to enter this manually every time you start your laptop, this command must be placed in your .bashrc file using following command:

echo "xhost +127.0.0.1 > /dev/null" >> /home/$USER/.bashrc

Now restart your laptop and try pulling a plug on AC adapter from your laptop. If it works great, if not comment here and maybe me or someone else can assist. Wish you all best!

DevGenii

A quality focused Magento specialized web development agency. Get in touch!

12 thoughts on “Ubuntu disable/enable compiz when on battery/AC bash script

  1. Marko Author

    @Teo Dumski

    Hi Theo! It was confusing from me to write my username marko when executing notify-send and “username” when switching using compiz and metacity replace. I’ve corrected this. Your usename is necessary because compiz i and metacity are run as non root (for every non root user) so we must switch them by doing su to your usename. Same for notify-send because we want it to pop out on your desktop so we execute it as su to your non root account. It should work fine with all “username” replaced by your own user name.

    Reply
  2. jason

    This script looked promising but it hosed my start up, I couldn’t get past the ubuntu logo.. I had to go into recovery mode and remove it! So just a word of caution save this page on another laptop in case you have to “undo” it…

    I am on Ubuntu 10.4

    Good luck ubuntuers

    Reply
    1. Marko Author

      Hello,
      yes it is not only possible but very easy to hose your startup by working with startup scripts. Do you happen to remember any error warning or notice Ubuntu spewed on you when it refused to start?

      Reply
  3. Camilo

    Hello,
    I know this is kind of old but if you could help me that would be great…
    I want to do the samething but using conky this is what i got based on your post:

    #!/bin/bash
     
    export DISPLAY=:0.0
    if on_ac_power; then
    	su -p -c 'notify-send -u critical -i "notification-message-IM" "On AC: Desktop compositing" "Turning desktop composition on"' warlock
    	sleep 10
    	su -p -c 'conky' warlock
     
    else
    	su -p -c 'notify-send -u critical -i "notification-message-IM" "On battery: Desktop compositing" "Turning desktop composition off"' warlock
    	sleep 1
    	su -p -c 'killall -v conky' warlock
    fi
     
    exit 0

    if i run conky form the terminal and then disconnect the AC adapter it works, it closes.
    But if i put back the AC adapter conky does not appear….
    Thanks!

    Reply
        1. Camilo
          #!/bin/bash
          export XAUTHORITY="/home/warlock/.Xauthority"
          export DISPLAY=:0.0
          if on_ac_power; then
              su -p -c 'notify-send -u critical -i "notification-message-IM" "On AC:  Turning conky on"' warlock
              sleep 10
              su -p -c 'conky' warlock
           
          else
              su -p -c 'notify-send -u critical -i "notification-message-IM" "On battery:  Turning conky off"' warlock
              sleep 1
              su -p -c 'killall -v conky' warlock
          fi
           
          exit 0

          I also added an export XAUTHORITY but it’s still not working.

          Reply
          1. Marko Author

            You could check is conky refusing to start (due to not finding display or whatever) or this if branch never executes. You can use ‘logger’ to log to syslog. Open gnome-system-log to view syslog or do tail -f /var/log/syslog fom terminal to monitor syslog and try disconnecting and connecting AC. Syslog should log messages issued by logger.

            #!/bin/bash
            export DISPLAY=:0.0
            if on_ac_power; then
                logger 'AC detected, trying to start conky'
                su -p -c 'notify-send -u critical -i "notification-message-IM" "On AC:  Turning conky on"' warlock
                sleep 10
                su -p -c 'conky' warlock
             
            else
                logger 'Battery detected, trying to start conky'
                su -p -c 'notify-send -u critical -i "notification-message-IM" "On battery:  Turning conky off"' warlock
                sleep 1
                su -p -c 'killall -v conky' warlock
            fi
             
            exit 0

            Also you could try increasing or decreasing sleep for on_ac_power branch to more or less than my 10 seconds to see if it makes any difference.

            Reply
            1. Camilo

              I get the log message but still nothing happens and nothing else appears. This is what I get:

              Sep 16 13:46:25 Inspiron warlock: AC detected, trying to start conky

              Reply
              1. Marko Author

                It is obvious that conky doesn’t start due to error you can’t see because it i run using su, usually this is when application can’t find display. You should find a way to redirect conky output to syslog so you could see why it is not starting. Sorry but I’m out of ideas. 🙂

                Reply

Leave a Reply

Your email address will not be published. Required fields are marked *