Wed, 31 Dec 2008

Taming the fox

As with most people these days, I have firefox running at all times. I also usually have about 50-100 tabs open (how I manage that insanity should probably be left for it's own blog post). When I'm not actively using it, firefox "idles" about as well as an ADHD kid after a case of Red Bull, and ends up waking my kernel up hundreds of times a second. When I'm hacking in vim, do I really want or need javascript/flash/animations/etc to be running? Probably not...

So tonight I threw together a little script to "solve" this "problem".

#!/bin/bash
# A tool for putting firefox to sleep when it does not have focus.
alive=1
while true; do
    if xwininfo -id $(xdotool getactivewindow) | egrep -q '(Firefox|Vimperator)' ; then
        if [ ! $alive ]; then
            kill -CONT `pidof firefox`
            alive=1
        fi
    else
        if [ $alive ]; then
            kill -STOP `pidof firefox`
            alive=
        fi
    fi
    sleep 1
done

Obviously the most efficient way to do this would be to hook into X's focus events, but I'm lazy...
*UPDATE*: I got un-lazy and implemented a better version using the python-xlib module. See Taming the fox (part 2).

This script uses xdotool, which I just packaged and pushed into review for Fedora. (update: Oops, looks like xdotool is already in Fedora :)


posted at: 04:23 | link | Tags: , | 10 comments

Posted by Colin Walters at Wed Dec 31 08:31:13 2008

Cool hack.  It's really something that should be part of Firefox itself, though doing it inside the firefox process would be quite tricky though.

Of course, you probably want to stop Flash npviewer.bin processes too.

Random general note: if you are an application author and want to do some sort of heavy lifting only when you're the foreground application, connecting to the "notify::active" in a Gtk.Window will do the right thing.  I use this in HotSSH to enable/disable connection status polling.

Posted by Luke at Wed Dec 31 08:52:38 2008

@Colin: My F10-x86_64 setup no longer uses the npviewer.bin, as flash seems to be native now -- but yeah, the script could easily be modified to SIGSTOP that as well.

Posted by Peter at Wed Dec 31 10:44:04 2008

It is not such a good idea, let alone having it in firefox itself.
In those modern web applications, you know, there is this javascript running that makes bunch of stuff, one being making XMLHTTPreuqests to the server on sites requiring you being logged in in order to work. Even worse, real time applications like meebo keep the http1.1 connection open at all time, so the result after 5 minutes ff being not the active window will be you being logged out.
On the other side if all those open tabs and windows do not require the application to be really running this will also mean they do not require to be loaded, i solved my problem with many many open tabs by using "read it later" extension and I just mark the stuff i want to keep handy and then close it.

Posted by Luke at Wed Dec 31 16:43:54 2008

Yes, Peter, this will not play well with "real time" web applications, as persistent XMLHttpRequest's would most likely timeout.  Your average person does not, and should not care about what Firefox is doing in the background.  This hack is merely for geeks who want to save power or CPU cycles under certain circumstances.  Such people should also understand the implications of such actions.

I don't think this behavior should be the default in Firefox either, as I am a big fan of live web applications, and I think they are going to become even more prevalent in the future.

Thanks for the "read it later" tip, I'll take a look at that Firefox extension...

Posted by Luke at Wed Dec 31 21:20:12 2008

Also, another caveat of this approach:  It will send firefox a SIGSTOP when a password dialog pops up.  You can resolve this by running 'kill -CONT `pidof firefox`'.  Workaround suggestions welcome :)

Posted by Saikat Guha at Wed Dec 31 21:36:21 2008

Interesting hack. One problem though is with the script getting confused when there is a local firefox copy running as well as a remote firefox through X-forwarding. It tries to SIGSTOP/SIGCONT the remote pid, and crashes out when os.kill throws an exception.

Posted by Ellie at Sun Jun 12 04:05:59 2011

Great common sense here. Wish I’d thoghut of that.

Posted by Bear at Sun Jun 12 08:33:55 2011

Now I know who the bainry one is, I’ll keep looking for your posts.

Posted by Cindy at Sun Jun 12 19:59:58 2011

Hey, you’re the goto expert. Tnhaks for hanging out here.

Posted by Debra at Mon Jun 13 15:51:04 2011

Your answer was just what I ndeeed. It’s made my day!


Name:


E-mail:


URL:


Comment: