I need to execute this:

rm -rf ~/.wine-pipelight/*;
rm -rf ~/.wine-pipelight/./.*;
cp -a ~/viewright_backup/. ~/.wine-pipelight

every time the Firefox window closes. But not necessarily when all the windows closed, but on every window that is closed. For example, if I have one Firefox window and one Firefox pop-up window. If I close at least one windows, I want to execute this command. Is this possible? Thanks!

  • 1
    Why do you want to do this? It would most probably be easier to solve the issue than using such a workaround. Which Pipelight and wine-compholio version do you use? Jul 6, 2014 at 17:31
  • I followed this toutorial. it's in serbian, but there are codes used in the description. Jul 6, 2014 at 18:13
  • It's one service of Serbia broadband company which is not oficially supported on linux. Jul 6, 2014 at 18:15

1 Answer 1


The only way I can think of is not very elegant. You can have a script running in the background that counts the number of open firefox windows every second and launches your command if that number changes. Something like:

#!/usr/bin/env bash

## Run firefox
/usr/bin/firefox &

## Initialize the variable to 100

## Start infinite loop, it will run while there
## is a running firefox instance.
while pgrep firefox >/dev/null;
    ## Get the number of firefox windows    
    num=$(xdotool search --name firefox | wc -l)

    ## If this number is less than it was, launch your commands
    if [ "$num" -lt "$last" ]
        rm -rf ~/.wine-pipelight/*;
        ## I included this since you had it in your post but it
        ## does exactly the same as the command above.
        rm -rf ~/.wine-pipelight/./.*;
        cp -a ~/viewright_backup/. ~/.wine-pipelight      

    ## Save the number of windows as $last for next time

    ## Wait for a second so as not to spam your CPU.
    ## Depending on your use, you might want to make it wait a bit longer,
    ## the longer you wait, the lighter the load on your machine
    sleep 1


Save the script above as firefox, put it in your ~/bin directory and make it executable chmod a+x ~/bin/firefox. Since Ubuntu adds ~/bin to your $PATH by default and adds it before any other directories, running firefox will launch that script instead of the normal firefox executable. Now, because the script is launching /usr/bin/firefox, this means that your normal firefox will appear, just as you expect, only with the script running as well. The script will exit as soon as you close firefox.


This script is

  1. Not elegant, it needs to be run as an infinite loop in the background.
  2. Requires xdotool, install it with sudo apt-get install xdotool
  3. Does not work for tabs, only windows.
  • nice workaround
    – kenn
    Jul 7, 2014 at 17:11
  • Thanks! I won't make it autostart, but rather make it start when firefox starts. Thanks you very much, I'll try it. Jul 8, 2014 at 8:46
  • @terdon Since I'm planing to make it start when firefox starts, is it possible to make it stop when firefox is closed. Maybe something like: while ["0" -lt "$(xdotool search --name firefox | wc -l)" ]. I don't know if sintax is right but you get the point... Could you rewrite it to meet this condition? Thanks! Jul 8, 2014 at 13:27
  • @DusanMilosevic see updated answer. I rewrote it so that the script itself launches firefox and only runs as long as at least one firefox instance is running.
    – terdon
    Jul 8, 2014 at 13:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .