默認chrome緩存位置在~/.cache/google-chrome中,磁盤io很多。為減少磁盤io,保護磁盤同時加快chrome速度,可設置緩存使用內存盤,缺點是重啟后緩存丟失,所以這里可以使用腳本進行同步。
1. 開機自動掛載內存盤 (使用/dev/shm, 無需自己創建內存盤)
sudo mkdir /ramdisk
sudo chmod 777 /ramdisk
sudo vim /etc/fstab
, 添加如下內容:# ramdisk none /ramdisk tmpfs nodev,nosuid,noatime,mode=1777,size=512M 0 0
可使用
df -h
命令查看 /ramdisk 虛擬分區大小。
2. 緩存同步(打包解包)腳本
首先需要安裝 tar 的 lzop 插件,然后建立核心腳本:
mkdir -p /home/dylanchu/scripts/chrome
touch /home/dylanchu/scripts/chrome/chromecache
chmod +x /home/dylanchu/scripts/chrome/chromecache
vim /home/dylanchu/scripts/chrome/chromecache
內容如下:
#!/usr/bin/sh
# invoke this after reboot and before shutdown
# make sure that you already have 'lzop' installed on your system
case "$1" in
import)
cd /dev/shm
tar --lzop -pxf /home/dylanchu/.cache/chromecache-backup.tar.lzop
;;
dump)
cd /dev/shm
# delete files larger than 3MB
find ./google-chrome/ -size +3M -exec rm {} \;
tar --lzop -pcf /home/dylanchu/.cache/chromecache-backup.tar.lzop google-chrome/
;;
*)
echo -e "Usage: $(cd `dirname $0`; pwd)/chromecache {import|dump}"
exit 1
;;
esac
exit 0
3. 開機導入腳本
開機時設置緩存路徑,及從壓縮包導入緩存
touch /home/dylanchu/scripts/chrome/onboot.sh
chmod +x /home/dylanchu/scripts/chrome/onboot.sh
vim /home/dylanchu/scripts/chrome/onboot.sh
內容如下:
#!/bin/sh
#for the google chrome cache
/bin/rm ~/.cache/google-chrome -R
/bin/mkdir -p /dev/shm/google-chrome
/bin/ln -sf /dev/shm/google-chrome ~/.cache/google-chrome
#for the chromium cache
#/bin/rm ~/.cache/chromium
#/bin/mkdir -p /dev/shm/chromium
#/bin/ln -sf /dev/shm/chromium ~/.cache/chromium
# import dumped cache file to ram:
echo [`date +"%Y-%m-%d %H:%M"`] On boot - Importing caches to ram >> /home/dylanchu/chromecache_sync.log
/home/dylanchu/scripts/chrome/chromecache import >> /home/dylanchu/chromecache_sync.log
echo [`date +"%Y-%m-%d %H:%M"`] On boot - Caches imported to ram >> /home/dylanchu/chromecache_sync.log
添加上述 onboot.sh 腳本到開機自啟動:
這里用xfce gui的 “會話和啟動” (session-settings),點擊添加,並設置名稱和腳本路徑。
(也可使用crontab的@reboot執行)
4. 關機前導出緩存到硬盤
關機前需要執行的腳本
touch /home/dylanchu/scripts/chrome/onshutdown.sh
chmod +x /home/dylanchu/scripts/chrome/onshutdown.sh
vim /home/dylanchu/scripts/chrome/onshutdown.sh
內容如下:
#!/bin/sh
# dump cache files from ram to disk:
echo [`date +"%Y-%m-%d %H:%M"`] On shutdown - Dumping caches to disk >> /home/dylanchu/chromecache_sync.log
/home/dylanchu/scripts/chrome/chromecache dump >> /home/dylanchu/chromecache_sync.log
echo [`date +"%Y-%m-%d %H:%M"`] On shutdown - Caches dumped to disk >> /home/dylanchu/chromecache_sync.log
ping -c 3 127.1 > /dev/null
讓 systemd 在關機時自動執行上述腳本
sudo vim /lib/systemd/system/chromedumpcache.service
內容如下:(測試無效)
[Unit]
Description=Dump chrome caches from ram to disk at shutdown.
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target
[Service]
Type=simple
RemainAfterExit=true
# when system start
ExecStart=/bin/true
# when system shutdown
ExecStop=/home/dylanchu/scripts/chrome/onshutdown.sh
[Install]
WantedBy=multi-user.target halt.target reboot.target shutdown.target
修改:
multi-user.target是字符界面,改為graphical.target后正常工作:(測試發現僅關機和重啟時工作)
[Unit]
Description=Dump chrome caches to disk
DefaultDependencies=no
Before=umount.target shutdown.target reboot.target halt.target
[Service]
Type=simple
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/home/dylanchu/scripts/chrome/onshutdown.sh
[Install]
WantedBy=graphical.target
在 systemd 注冊之:
sudo systemctl enable chromedumpcache.service
sudo systemctl daemon-reload
sudo systemctl status chromedumpcache.service
systemctl get-default
命令可以查看系統啟動默認進入哪個界面
5. 重啟生效。
參考:
https://wiki.archlinux.org/index.php/Tmpfs
https://wiki.archlinux.org/index.php/Chromium/Tips_and_tricks#Cache_in_tmpfs內存盤和硬盤同步:
http://docs.observium.org/persistent_ramdisk/
https://askubuntu.com/questions/416299/execute-command-before-shutdown-reboot(用systemd關機前執行指令)
http://blog.csdn.net/kai165416/article/details/79449638 (刪除大於固定大小的文件)https://askubuntu.com/questions/794290/create-ramdisk-on-16-04-for-chrome
https://www.omgubuntu.co.uk/2010/11/move-google-chrome-cache-to-ramdisk
http://www.bubuko.com/infodetail-1900178.html
http://blog.51cto.com/10237569/1871723
https://superuser.com/questions/1016827/how-do-i-run-a-script-before-everything-else-on-shutdown-with-systemd
http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html
https://www.freedesktop.org/software/systemd/man/bootup.html#System Manager Bootup (systemd 啟動順序)