wmctrl not working in crontab

Tag: linux , shell Author: mimoe Date: 2013-12-24

I'm using Linux (Ubuntu). I use wmctrl to make the firefox window always on top. And it worked FINE when I run the shell on a terminal.

Here is my shell code (say that it was /usr/app/keepfront.sh):

WINTITLE="Mozilla Firefox" # Main Firefox window has this in titlebar
PROGNAME="firefox mywebsite --sync" #run the firefox program

#Use wmctrl to list all windows, count how many contain WINTITLE
WINCOUNT=wmctrl -l | grep -c "$WINTITLE"

if [ $WINCOUNT != 0 ]
    wmctrl -a "$WINTITLE" # If it exists, bring window to front
    $PROGNAME & # Otherwise, just launch ff
exit 0

I would like to use crontab to run the shell every 1 minute. Crontab DID run the shell (I wrote some echos), but nothing happened.

Here is my crontab code:

*/1 * * * *   /usr/app/keepfront.sh

Anyone know WHY? How to solve this?

Other Answer1

One part of your problem is that this line doesn't do what you think it does:

WINCOUNT=wmctrl -l | grep -c "$WINTITLE"

It runs the command -l (which probably doesn't exist) with WINCOUNT=wmctrl as one of its environment variables.

You probably intended to write:

WINCOUNT=$(wmctrl -l | grep -c "$WINTITLE")

The other part of your problem may be that wmctrl and firefox don't work correctly when run without a terminal, as crontab runs its jobs without a terminal. I've not tried running firefox from crontab, and I can't think of anything much more annoying than having Firefox jump to the foreground every minute (OK; I can think of some things about equally annoying, but the concept doesn't bear thinking about).


You're right about the first point. WINCOUNT=$(wmctrl -l | grep -c "$WINTITLE") works and plus looks nicer. While I still need to keep ff always on top,do u have any idea?