Ubuntu – Can I run shell script together with application

background-processbashscripts

I wrote small script to automatically delete Chrome cache after I close it:

#!/bin/bash

while true; do
    if [[ $(pgrep -l chrome) ]]; then
        sleep 20
    else
        rm -rf ~/.cache/google-chrome/Default/Cache/*
        rm -rf ~/.cache/google-chrome/Default/"Media Cache"/*
        notify-send "CCD" "Cache deleted!"
        break    
fi
done

Now I don't want to run this script manually everytime, I want it to run in background automatically when I launch Chrome. I tried to edit Chrome quicklist with Ubuntu Tweak:
enter image description here

But as I expected, it didn't work. So, is there any other way to do it?

Best Answer

Just add full path to your script into Startup Applications to make it start automatically upon login. Open Unity Dash and add it as new command. Of course, make sure you script has executable permissions with chmod +x /path/to/script.sh

enter image description here

To address the issue of chrome deleting cache on startup ( which is undesirable , as mentioned in the comments), use polling with while-loop to wait for chrome to appear.

while true; do

    # Wait for chrome window to appear
    while ! pgrep -l 'chrome' ; do : ; sleep 20; done 

    # Now wait for it to disappear
    while pgrep -l 'chrome' ; do : ; sleep 20; done 

    # Once chrome window disappears, delete cache.
    rm -rf ~/.cache/google-chrome/Default/Cache/*
    rm -rf ~/.cache/google-chrome/Default/"Media Cache"/*
    notify-send "CCD" "Cache deleted!"

    # And at this point we restart the whole process again.

done
Related Question