mac 使用launchctl 开机时加速vim、emacs

8 篇文章 0 订阅
3 篇文章 0 订阅

WARNING

注意:脚本原文件来源于网络,部分修改只在小范围测试过,请在正式使用前做好备份、测试

目标

  1. 编写脚本,将vim,emacs等配置文件放置到内存中,调用速度更快
  2. 使用launchctl开机自启动1中的脚本

环境

脚本

mount-ram-vim.sh 创建内存镜像挂载点

#!/bin/sh

# This program has two feature.
#
# 1. Create a disk image on RAM.
# 2. Mount that disk image.
#
# Usage:
#   $0 <dir> <size>
#
#   size:
#     The `size' is a size of disk image (MB).
#
#   dir:
#     The `dir' is a directory, the dir is used to mount the disk image.
#
# See also:
#   - hdid(8)
#

mount_point=${1:-/Users/mac/vim_ram}
size=${2:-128}

mkdir -p $mount_point
if [ $? -ne 0 ]; then
    echo "The mount point didn't available." >&2
    exit $?
fi

sector=$(expr $size \* 1024 \* 1024 / 512)
device_name=$(hdid -nomount "ram://${sector}" | awk '{print $1}')
if [ $? -ne 0 ]; then
    echo "Could not create disk image." >&2
    exit $?
fi

newfs_hfs -v vim $device_name > /dev/null
if [ $? -ne 0 ]; then
    echo "Could not format disk image." >&2
    exit $?
fi

mount -o nobrowse,rw -t hfs $device_name $mount_point
# -o nobrowse : do not display mount device on Finder sidebar
if [ $? -ne 0 ]; then
    echo "Could not mount disk image." >&2
    exit $?
fi

unmount-ram-vim.sh 关机/重启时卸载内存镜像

#!/bin/sh

# This program has two features.
#
# 1. Unmount a disk image.
# 2. Detach the disk image from RAM.
#
# Usage:
#   $0 <dir>
#
#   dir:
#     The `dir' is a directory, the dir is mounting a disk image.
#
# See also:
#   - hdid(8)
#

mount_point=${1:-/Users/mac/vim_ram}
if [ ! -d "${mount_point}" ]; then
    echo "The mount point didn't available." >&2
    exit 1
fi
mount_point=$(cd $mount_point && pwd)

device_name=$(df "${mount_point}" 2>/dev/null | tail -1 | grep "${mount_point}" | cut -d' ' -f1)
if [ -z "${device_name}" ]; then
    echo "The mount point didn't mount disk image." >&2
    exit 1
fi

umount "${mount_point}"
if [ $? -ne 0 ]; then
    echo "Could not unmount." >&2
    exit $?
fi

hdiutil detach -quiet $device_name

vim2ram.sh 将spacevim配置文件移入、移出内存镜像

#!/bin/sh
function start()
{
    backup=/Users/mac/.spacevim-backup
    link=/Users/mac/.SpaceVim
    volatile=/Users/mac/vim_ram/.SpaceVim-$USER

    IFS=
    set -efu

    cd ~/

    if [ ! -r $volatile ]; then
        mkdir -m0700 $volatile
    fi

    # link -> volatie does not exist
    if [ "$(readlink $link)" != "$volatile" ]; then
        # backup project at first
        mv $link $backup
        # create the link
        ln -s $volatile $link
    fi

    if [ -e $link/.unpacked ]; then
        echo "Sync .SpaceVim from memory to backup ..."
        rsync -avq --delete --exclude .unpacked $link/ $backup/
        echo "DONE!"
    else
        echo "Sync .SpaceVim from disk to memory ..."
        rsync -avq $backup/ $link/
        touch $link/.unpacked
        echo "DONE!"
    fi
}

function restore()
{
    echo "Moving .SpaceVim back to disk ..."
    backup=/Users/mac/.spacevim-backup
    link=/Users/mac/.SpaceVim
    volatile=/Users/mac/vim_ram/.SpaceVim-$USER
    cd ~/
    rm -f $link && mv $backup $link && rm -rf $volatile
    echo "DONE!"
}

if [ -z "$1" ];then
    echo "-------------------------------------------------------------"
    echo "Usage:"
    echo "  vim2ram start"
    echo "  vim2ram restore"
    echo "Default action is 'vim2ram start'. Now excute start..."
    start
    echo "-------------------------------------------------------------"
    exit 1
fi

if [ "$1" == "start" ];then
    start
else
    restore
fi

launchctl开机服务启动

启动脚本 boot-shutdown.sh

#!/bin/sh
#
# Author: Vincenzo D'Amore v.damore@gmail.com
# 20/11/2014
#

function shutdown()
{
  echo `date` " " `whoami` " Received a signal to shutdown"

  # INSERT HERE THE COMMAND YOU WANT EXECUTE AT SHUTDOWN
  # remember Moving .emacs.d from ram back to disk
  ##emacs#
  #sh /Users/mac/emacs-speedup-scripts/emacs2ram.sh restore
  #echo ".emacd.d restore from ram complete!"
  #sh /Users/mac/emacs-speedup-scripts/unmount-ram.sh
  #echo "unmount-ram complete!"

  ##vim#
  sh /Users/mac/emacs-speedup-scripts/vim2ram.sh restore
  echo "SpaceVim restore from ram complete!"
  sh /Users/mac/emacs-speedup-scripts/unmount-ram-vim.sh
  echo "unmount-ram-vim complete!"

  exit 0
}

function startup()
{
  echo `date` " " `whoami` " Starting..."

  # INSERT HERE THE COMMAND YOU WANT EXECUTE AT STARTUP
  ##emacs#
  #sleep 3
  #sh /Users/mac/emacs-speedup-scripts/mount-ram.sh
  #echo "mount-ram complete!"
  #sh /Users/mac/emacs-speedup-scripts/emacs2ram.sh
  #echo "move .emacs.d to ram complete!"

  #vim#
  sleep 3
  sh /Users/mac/emacs-speedup-scripts/mount-ram-vim.sh
  echo "mount-ram-vim complete!"
  sh /Users/mac/emacs-speedup-scripts/vim2ram.sh
  echo "move SpaceVim to ram complete!"

  tail -f /dev/null &
  wait $!
}

trap shutdown SIGTERM
trap shutdown SIGKILL

startup;

launchctl基础概念

macOS开机启动一般使用launchctl加载plist文件

plist文件放置处:

~/Library/LaunchAgents 由用户自己定义的任务项
/Library/LaunchAgents 由管理员为用户定义的任务项
/Library/LaunchDaemons 由管理员定义的守护进程任务项
/System/Library/LaunchAgents 由Mac OS X为用户定义的任务项
/System/Library/LaunchDaemons 由Mac OS X定义的守护进程任务项

/System/Library和/Library和~/Library目录的区别?

/System/Library目录是存放Apple自己开发的软件。
/Library目录是系统管理员存放的第三方软件。
~/Library/是用户自己存放的第三方软件。

LaunchDaemons和LaunchAgents的区别?

LaunchDaemons是用户未登陆前就启动的服务(守护进程)。
LaunchAgents是用户登陆后启动的服务(守护进程)。

launchctl基础命令

本次配置plist文件在 ~/Library/LaunchAgents 目录

launchctl load ~/Library/LaunchAgents/example.plist
# 卸载配置
launchctl unload ~/Library/LaunchAgents/example.plist
# 检查语法是否正确
plutil ~/Library/LaunchAgents/example.plist
# 查看服务运行状态
launchctl list

plist文件:

1.在~/Library/LaunchAgents 创建文件Vim2Ram.plist,内容为:

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>boot_vim2ram</string>
    <key>Program</key>
    <string>/Users/mac/macosx-script-boot-shutdown/boot-shutdown.sh</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/mac/macosx-script-boot-shutdown/boot-shutdown.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>WorkingDirectory</key>
    <string>/Users/mac/macosx-script-boot-shutdown/</string>
    <key>StandardOutPath</key>
    <string>/Users/mac/macosx-script-boot-shutdown/boot-shutdown.log</string>
    <key>StandardErrorPath</key>
    <string>/Users/mac/macosx-script-boot-shutdown/boot-shutdown.err</string>
    <key>ExitTimeOut</key>
    <integer>600</integer>
</dict>
</plist>
  • /Users/mac/macosx-script-boot-shutdown/ 为自定义路径,按需修改
  • /Users/mac/macosx-script-boot-shutdown/boot-shutdown.err与log为自定义日志文件,可有可无
  • /Users/mac/macosx-script-boot-shutdown/boot-shutdown.sh 即启动脚本 1

效果

配置好plist之后,启动 launchctl load -w Vim2RAM.plist

查看镜像:

其他

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值