linux chrome 缓存,Linux设置chrome缓存至内存,及开关机同步

默认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. 重启生效。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值