我可以和应用程序一起运行shell脚本吗?

我写了一个小脚本,在我关闭它后自动删除Chrome缓存:

#!/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 

现在我不希望每次都手动运行此脚本,我希望它在启动Chrome时自动在后台运行。 我尝试使用Ubuntu Tweak编辑Chrome快速列表: 在此处输入图像描述

但正如我所料,它没有用。 那么,有没有其他方法可以做到这一点?

只需将脚本的完整路径添加到Startup Applications以使其在登录时自动启动。 打开Unity Dash并将其添加为新命令。 当然,请确保脚本具有chmod +x /path/to/script.sh可执行权限

在此处输入图像描述

要解决启动时chrome删除缓存的问题(这是不可取的,如评论中所述),请使用while循环轮询等待chrome出现。

 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