網站首頁 編程語言 正文
WARNING
注意:腳本原文件來源于網絡,部分修改只在小范圍測試過,請在正式使用前做好備份、測試
目標
- 編寫腳本,將vim,emacs等配置文件放置到內存中,調用速度更快
- 使用launchctl開機自啟動1中的腳本
環境
- mac osx 10.14.6
- vim8 配置文件為?SpaceVim
- emacs 配置文件為?seagle0128
腳本
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
查看鏡像:?
其他
- emacs處理邏輯類似,參照vim腳本修改即可
- 碰到plist報錯問題,可使用launchcontrol調試, 安裝命令為 brew cask install launchcontrol
參考:https://stackoverflow.com/questions/34215527/what-does-launchd-status-78-mean-why-my-user-agent-not-running
原文鏈接:https://blog.csdn.net/justheretobe/article/details/108737220
相關推薦
- 2021-11-06 Docker下部署lnmp詳細步驟_docker
- 2022-10-07 python?中pass和match使用方法_python
- 2022-09-24 Go?類型轉化工具庫cast函數詳解_Golang
- 2023-10-16 微信小程序radio單選按鈕選中與取消
- 2021-11-25 使用Xshell連接VMware上的Linux虛擬機(圖文步驟)_VMware
- 2024-01-15 IDEA 常量字符串過長問題
- 2023-01-23 C語言實現讀取CSV文件的方法詳解_C 語言
- 2022-04-28 C++Stack棧類模版實例詳解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支