Archive

Posts Tagged ‘bash’

Suspend out-of-focus Firefox to save battery from useless CPU usage

November 29th, 2014 3 comments

My Firefox (or rather Iceweasel) is prone to constantly spinning and eating about 50-70% CPU on average when should be supposed to just sit idle. I tried to find the root cause, but Javascript profiler sees nothing and other forays didn’t end up with much (I discovered that spinning progress wheel when some tab is forever loading amounts to about 20% CPU, though). Not sure if always, but sometimes it spins within WebGLImageConverter::run() (no callgraph, sorry).

So in the end I decided to treat the symptoms instead. Cleaning my CPU fan helped with the noise, but the main problem is that running firefox brings my battery life to about 1/2 to 1/3. So one obvious solution would be to just stop the damn process when I don’t use it. I typically don’t do background downloads while on battery (or otherwise), so that means I want it stopped when the window is inactive – not in focus. This is surprisingly exotic idea, apparently, and not easy to do in most window managers.

I even tried switching to awesome or i3 window managers which should make this easy, but I’m psychologically not up to that; I think I’m too conservative, but I decided not to stick with that. I use the MATE desktop environment with marco window manager. Perhaps switching to sawfish would be a good option, but in the end I just decided to write a shell script that will periodically assess the situation and suspend or resume firefox as needed. Of course this introduces extra wakeups and ambient CPU load, but when powertop reports that my GoogleTalkPlugin (running at all times for whatever reason) wakes up 150 times per second, the powersaving situation on Linux is still too messy – so who cares?

Here goes the script, in the hope that it will be useful for someone else too. Run it in a terminal or backgrounded in your ~/.xprofile, it will stop the firefox process when out of focus for more than 10s and on battery, and resume it within a second when switching back. In practice, I found these timings completely acceptable so far, and didn’t notice any ill effects of constant STOP/CONT either.

firefox-suspender.sh:

#!/bin/bash
#
# firefox-suspender: Periodically check whether firefox is out of focus
# and STOP it in that case after a time delay; if in focus but stopped,
# send SIGCONT.
#
# (c) Petr Baudis <pasky@ucw.cz>  2014
# MIT licence if this is even copyrightable
 
loop_delay=1    # [s]
stop_delay=10   # [s]
 
last_in_focus=$(date +%s)
firefoxpid=
state=running
 
while true; do
  sleep $loop_delay
 
  # Get active window id
  window=$(xprop -root _NET_ACTIVE_WINDOW)
  window=${window#*# }
  # What kind of window is it?
  class=$(xprop -id "$window" WM_CLASS)
  # echo Active window $window, class $class
 
  if [[ "$class" =~ Navigator ]]; then
    # Firefox!  We know it is running.  Make sure we
    # have its pid and update the last seen date.
    # If we stopped it, resume again.
    if [ "$state" = stopped ]; then
      echo "$(date)  Resuming firefox @ $firefoxpid"
      if kill -CONT $firefoxpid; then
        state=running
      else
        firefoxpid=
      fi
    fi
    last_in_focus=$(date +%s)
    if [ -z "$firefoxpid" ]; then
      firefoxpid=$(pidof iceweasel)
    fi
    if [ -z "$firefoxpid" ]; then
      firefoxpid=$(pidof firefox)
    fi
 
    continue
  fi
 
  # Not Firefox!  If it's running, we are on battery and
  # it's been long enough, stop it now.
  if [ "$state" != running ]; then
    continue
  fi
 
  read battery </sys/class/power_supply/BAT0/status
  if [ "$battery" != Discharging ]; then
    continue
  fi
 
  if [ $(($(date +%s) - last_in_focus)) -ge $stop_delay ]; then
    echo "$(date)  Stopping firefox @ $firefoxpid"
    if ! kill -STOP $firefoxpid; then
      firefoxpid=
    fi
    state=stopped
  fi
done

An improved version of this script: https://github.com/mkoura/browser-suspender

Categories: linux Tags: , , , ,

GPS souřadnice českých měst a obcí

February 1st, 2014 14 comments

Pro zobrazování poloh dopadů meteosond na IRC jsem potřeboval v jednoduchém CSV formátu seznam souřadnic českých měst, ale ukázalo se, že je překvapivě obtížné něco takového získat. Sice existuje tabulka na jednom astronomickém webu, výběr tam zahrnutých obcí je ale docela divný, někde je místo obce jen její část, atd.

Nakonec jsem zvolil postup “udělej si sám”, a to kombinací seznamu na Wikipedii, Google Geocoding API a trochy XPath.

Seznam rozumné podmnožiny měst mohu získat třeba pomocí:

curl 'http://cs.wikipedia.org/w/index.php?title=Seznam_obc%C3%AD_s_roz%C5%A1%C3%AD%C5%99enou_p%C5%AFsobnost%C3%AD&action=edit' |
  sed -ne 's/^# \[\[\([^]|]*|\)*\([^]]*\)\]\].*/\2/p' | sort

Mám-li zase jméno obce, její souřadnice mohu získat tímto zaklínadlem:

m=Aš; curl -s 'http://maps.googleapis.com/maps/api/geocode/xml?address='"${m// /+},+CZ"'&sensor=false' |
  xmllint --xpath '//location[lat or lng]//text()' -

(Důležitý trik je to ,CZ, jinak bude Google znát spoustu Kolínů a Aš bude znamenat Americká Samoa. Alternativně si můžete z výsledků vyfiltrovat ty české pomocí XPath //result[address_component/short_name/text()="CZ"]/geometry/location[lat or lng]//text().)

Teď už to pro vygenerování jednoduchého CSV stačí spojit dohromady:

curl 'http://cs.wikipedia.org/w/index.php?title=Seznam_obc%C3%AD_s_roz%C5%A1%C3%AD%C5%99enou_p%C5%AFsobnost%C3%AD&action=edit' |
  sed -ne 's/^# \[\[\([^]|]*|\)*\([^]]*\)\]\].*/\2/p' | sort |
  while read m; do
    echo -n $m
    curl -s 'http://maps.googleapis.com/maps/api/geocode/xml?address='"${m// /+},+CZ"'&sensor=false' |
      xmllint --xpath '//location[lat or lng]//text()' - |
      tr -s '\n' ' ' | tr ' ' ','
    echo
    sleep 0.1
  done | sed 's/,$//'

Rádi byste hotové CSV?

Bonus: Podobně vygenerované CSV s pražskými částmi (katastrálními územími).

Bonus 2: A ještě CSV s obcemi s přenesenou působností (další velké obce a města)

Categories: linux Tags: , , , , ,

Weathersonde – Nearby Landing Notification

January 26th, 2014 1 comment

At our hackerspace brmlab, one of the things we do is picking up landed weather sondes. In short, fun hardware literally falling off the sky, several times a day, every day. These are stratospheric balloons used for weather data prediction, launched from various sites, that reach the 35km altitude, then the balloon bursts and it lands back on the ground at a random location. At the whole time, it transmits its current GPS coordinates via radio, making this a rather exciting sub-class of geocaching.

As a simple hack today (idea by chido), I created a simple script sonde.sh that is designed to be run three times a day, runs sonde trajectory prediction (a predict.habhub.org service – example) and if the sonde is predicted to land in a certain radius, reports that with a link to the prediction. By default, it is connected to jendabot, one of our brmlab IRC robotic minions, written in an appealingly crazy way as a collection of bash scripts.

Categories: life, software Tags: , , , , , ,