创建定时任务的范例文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  3. <plist version="1.0">
  4. <dict>
  5. <!-- 名称,要全局唯一 -->
  6. <key>Label</key>
  7. <string>php.schedule.scsys</string>
  8. <!-- 脚本任务 -->
  9. <key>ProgramArguments</key>
  10. <array>
  11. <string>/usr/local/Cellar/php@7.2/7.2.32/bin/php</string>
  12. <string>/Users/admin/Sites/scsys_admin/artisan</string>
  13. <string>schedule:run</string>
  14. </array>
  15. <!-- 运行间隔,与StartCalenderInterval使用其一,单位为秒 -->
  16. <key>StartInterval</key>
  17. <integer>60</integer>
  18. <!-- 标准输入文件 -->
  19. <key>StandardInPath</key>
  20. <string>/Users/admin/.config/schedule/scsys-in.log</string>
  21. <!-- 标准输出文件 -->
  22. <key>StandardOutPath</key>
  23. <string>/Users/admin/.config/schedule/scsys-out.log</string>
  24. <!-- 错误输出 -->
  25. <key>StandardErrorPath</key>
  26. <string>/Users/admin/.config/schedule/scsys-err.log</string>
  27. </dict>
  28. </plist>

Plist 目录

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

参数说明

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  3. <plist version="1.0">
  4. <dict>
  5. <!-- 名称,要全局唯一 -->
  6. <key>Label</key>
  7. <string>com.uniflor.notifier</string>
  8. <!-- 要运行的程序, 如果省略这个选项,会把ProgramArguments的第一个
  9. 元素作为要运行的程序 -->
  10. <key>Program</key>
  11. <string>/Users/uniflor/script.sh</string>
  12. <!-- 命令, 第一个为命令,其它为参数-->
  13. <key>ProgramArguments</key>
  14. <array>
  15. <string>/Users/uniflor/script.sh</string>
  16. </array>
  17. <!-- 运行时间 -->
  18. <key>StartCalendarInterval</key>
  19. <dict>
  20. <key>Minute</key>
  21. <integer>30</integer>
  22. <key>Hour</key>
  23. <integer>9</integer>
  24. <key>Day</key>
  25. <integer>1</integer>
  26. <key>Month</key>
  27. <integer>5</integer>
  28. <!-- 0和7都指星期天 -->
  29. <key>Weekday</key>
  30. <integer>0</integer>
  31. </dict>
  32. <!-- 运行间隔,与StartCalenderInterval使用其一,单位为秒 -->
  33. <key>StartInterval</key>
  34. <integer>30</integer>
  35. <!-- 标准输入文件 -->
  36. <key>StandardInPath</key>
  37. <string>/Users/uniflor/run-in.log</string>
  38. <!-- 标准输出文件 -->
  39. <key>StandardOutPath</key>
  40. <string>/Users/uniflor/Bin/run-out.log</string>
  41. <!-- 标准错误输出文件 -->
  42. <key>StandardErrorPath</key>
  43. <string>/Users/uniflor/Bin/run-err.log</string>
  44. </dict>
  45. </plist>

加载命令

  1. # 加载任务, -w选项会将plist文件中无效的key覆盖掉,建议加上
  2. launchctl load -w com.demo.plist
  3. # 删除任务
  4. launchctl unload -w com.demo.plist
  5. # 查看任务列表, 使用 grep '任务部分名字' 过滤
  6. launchctl list | grep 'com.demo'
  7. # 开始任务
  8. launchctl start com.demo.plist
  9. # 结束任务
  10. launchctl stop com.demo.plist

参考链接

launchctl :MAC 下的定时任务
Mac中的定时任务利器:launchctl
mac 自动定时执行任务