前言
对于一些系统以及项目依赖, 个人都有跟进的习惯;
Macos 内置 launchctl 来写定时任务.. 不会...
还是选择了 Linux 常用的 crontab;
问题
这问题可能是系统安全策略的问题. 我一开始尝试了好久;
不管是用 Root 还是自身用户.. 都会报 Operation not permitted(任务没法写入);
开始没想到是系统版本太新 (安全策略) 的问题, 实在没辙只能去找来同事一起排查, 排除了语法还是其他一些问题,
还是有问题, 我说要不去你电脑试试, 他的是 10.13(能正常运行). 于是开始谷歌大法, 终于在国外的论坛也有遇到类似的问题..
有说到关闭 Rootless 机制的, 也有说添加硬盘操作权限的, 我选择了后者;
关闭 Rootless 的机制 https://www.leocode.net/article/index/20.html
我没试过这个姿势.. 有兴趣的自己试试..
添加硬盘访问权限的, 就是把你的终端加进去这里
Crontab 的姿势
crontab 的用法还是很简单的
-e: 就是进入编辑
-u: 指定用户
-l: 列出调度清单
-r: 删除调度任务
不带用户参数都是都是基于当前用户来启用 crontab 控制
语法格式: 分钟 小时 日 月 周 command shell_file
- # demo
- */5 * * * */bin/zsh /Users/linqunhe/Code/u-logs/autoShell.sh
- # / 是每隔, 还支持 , - 这类指定和连续的.. 具体看对应的手册吧
脚本的权限记得设置好, 比如你指定普通用户的, 744 便足以
- r:4 , w:2 ,x : 1 ; u-g-o(自己 / 组 / 其他)
- -rwxr--r--@ 1 linqunhe staff 867B Dec 7 14:50 autoShell.sh
我的调度需求很简单, 就是更新一些东西, 有涉及到 alias , 用到了 zsh(因为写在. zshrc)
- #!/bin/zsh
- source ~linqunhe/.zshrc
- logPath="/Users/linqunhe/Code/u-logs/logs/$(date +'%Y-%m-%d').log"
- echo '-------- 更新脚本开始走起 --------------'>> $logPath
- # 更新 NG 项目
- cd /Users/linqunhe/Code/ng-sx-pc
- ng update --all 2>&1>> $logPath
- echo '---------Ng Update End-------------'>> $logPath
- # 更新 nuxt 项目
- cd /Users/linqunhe/Code/nuxt2-sx-mobile-share
- yarn outdated 2>&1>> $logPath
- yu
- echo '---------Nuxt Update End-------------'>> $logPath
- # 更新 umi 项目(yu 是我写的 alias)
- cd /Users/linqunhe/Code/umi_dva_sx_admin
- yarn outdated 2>&1>> $logPath
- yu
- echo '---------Umi Update End-------------'>> $logPath
- # 更新老的 react-sx-admin
- cd /Users/linqunhe/Code/react-sx-admin
- yarn outdated 2>&1>> $logPath
- yu
- echo '---------react-sx-admin Update End-------------'>> $logPath
- # 更新全局的 yarn npmyarn
- ygu 2>&1>> $logPath
- echo '---------yarn global Update End-------------'>> $logPath
- # 列出全局的 NPM 包
- ygl 2>&1>> $logPath
- echo '---------yarn global list End-------------'>> $logPath
- # 更新 brew && brew cask
- brew prune
- brew update 2>1>> $logPath
- brew upgrade 2>&1>> $logPath
- brew cask upgrade 2>&1>> $logPath
- echo '----------Brew && Brew Cask Update End------------'>> $logPath
- # 更新 Mac 系统
- softwareupdate -i -a 2>&1>> $logPath
- echo '--------Mac OS Update End--------------'>> $logPath
- echo '-------- 更新脚本结束 --------------'>> $logPath
是 1, 所以 ">/dev/null" 等同于 "1>/dev/null"
- # 2 : 表示 stderr 标准错误
- # & : 表示等同于的意思, 2>&1, 表示 2 的输出重定向等同于 1
输出就这样了
2018-12-07.log
总结
为什么在本本写任务调度, 因为笔记本常年不关机.. 基本都是待机状态, 开盖即用..
有不对之处请留言, 会及时修正. 谢谢阅读
来源: https://juejin.im/post/5c0a12c66fb9a049ea38c276