教程 launchctl 定时任务 利用Mac下的launchctl(定时任务)来备份数据到google盘 Sandy 2019-07-12 2019-10-16 利用Mac下的launchctl(定时任务)来备份数据到google盘 1. 说明 由于sandy用的是黑苹果,未知的状态太多。所以得备份。时光机器可以解决为什么还要launchctl来备份吗?两手备份是最安全的,吃过这样的亏上过这样的当。还是多一个备份渠道还是好滴!
2. 开始制作 2.1 创建备份脚本.sh脚本 进入~/Users/用户名称/ 程序所在目录
Bash
Copy
创建imacdownbak.sh脚本
Bash
Copy
添加下面脚本,修改相关的配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 #!/bin/bash WEB_DATA=/Users/sandyliao/Downloads WebBakName=imac_Downloads_$(date +%Y%m%d).tar.gz OldWeb=imac_Downloads_$(date -d -10day +"%Y%m%d" ).tar.gz rm -rf /Users/sandyliao/.CMVolumes/home\ sandy/imac/imac_Downloads_$(date -d -1day +"%Y%m%d" ).tar.gzcd /Users/sandyliao/Downloadstar zcf /Users/sandyliao/Downloads/$WebBakName $WEB_DATA mv /Users/sandyliao/Downloads/$WebBakName /Users/sandyliao/.CMVolumes/home\ sandy/imac/echo “imac备份结束”
Bash
Copy
Bash
Copy
注意,脚本要改成可执行的权限
1 chmod 777 imacdownbak.sh
Bash
Copy
2.2 2 编写plist文件 launchctl 将根据plist文件的信息来启动任务。 plist脚本一般存放在以下目录:
/Library/LaunchDaemons –>只要系统启动了,哪怕用户不登陆系统也会被执行
/Library/LaunchAgents –>当用户登陆系统后才会被执行
更多的plist存放目录:
1 2 3 4 5 ~/Library/LaunchAgents 由用户自己定义的任务项 /Library/LaunchAgents 由管理员为用户定义的任务项 /Library/LaunchDaemons 由管理员定义的守护进程任务项 /System/Library/LaunchAgents 由Mac OS X为用户定义的任务项 /System/Library/LaunchDaemons 由Mac OS X定义的守护进程任务项
Shell
Copy
进入 /Library/LaunchAgents
1 cd /Library/LaunchAgents
Bash
Copy
创建一个plist文件 com.imacdownbak.plist
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 vim com.imacdownbak.plist <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <!-- Label唯一的标识 --> <key>Label</key> <string>com.imacdownbak.plist</string> <!-- 指定要运行的脚本 --> <key>ProgramArguments</key> <array> <string>/Users/sandyliao/imac-down-bak.sh</string> </array> <!-- 指定要运行的时间 每天凌晨3点运行 --> <key>StartCalendarInterval</key> <dict> <key>Minute</key> <integer>00</integer> <key>Hour</key> <integer>03</integer> </dict> <!-- 标准输出文件 --> <key>StandardOutPath</key> <string>/Users/sandyliao/imac-down-bak.log</string> <!-- 标准错误输出文件,错误日志 --> <key>StandardErrorPath</key> <string>/Users/sandyliao/imac-down-bak.err</string> </dict> </plist>
XML
Copy
2.2.3 加载命令 1 launchctl load -w com.imacdownbak.plist
这样任务就加载成功了。
2.2.4 更多的命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 $ launchctl load -w com.demo.plist $ launchctl unload -w com.demo.plist $ launchctl list | grep 'com.demo' $ launchctl start com.demo.plist $ launchctl stop com.demo.plist
Bash
Copy
3 番外篇 plist支持两种方式配置执行时间:
StartInterval: 指定脚本每间隔多长时间(单位:秒)执行一次;
StartCalendarInterval: 可以指定脚本在多少分钟、小时、天、星期几、月时间上执行,类似如crontab的中的设置,包含下面的 key:
1 2 3 4 5 6 7 8 9 10 Minute <integer > The minute on which this job will be run. Hour <integer > The hour on which this job will be run. Day <integer > The day on which this job will be run. Weekday <integer > The weekday on which this job will be run (0 and 7 are Sunday). Month <integer > The month on which this job will be run.
Bash
Copy
plist部分参数说明
Label:对应的需要保证全局唯一性;
Program:要运行的程序;
ProgramArguments:命令语句
StartCalendarInterval:运行的时间,单个时间点使用dict,多个时间点使用 array
StartInterval:时间间隔,与StartCalendarInterval使用其一,单位为秒
StandardInPath、StandardOutPath、StandardErrorPath:标准的输入输出错误文件,这里建议不要使用 .log 作为后缀,会打不开里面的信息。
定时启动任务时,如果涉及到网络,但是电脑处于睡眠状态,是执行不了的,这个时候,可以定时的启动屏幕就好了。
更多的参数参见:mac官方文档