软件测试之ios自动化——Mac下的定时任务(launchctl)

在做ios自动化的过程中,通常离不开mac,需要在mac上开启定时任务。mac下有两种开启定时任务的方式:

  • 一种是使用crontab
  • 一种是使用Schedule

crontab是linux下的定时任务,在mac os上不推荐使用crontab,倾向于推荐使用Plist的方法。接下来重点介绍一下Plist的使用方法。

1、plist文件的位置

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

2、plist文件编写

先看一个plist任务,然后在详细解释里面字段的含义,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.haleli.MonkeyRunner.Daemon</string>
  <!-- 指定要运行的脚本 -->
  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/bin/python</string>
    <string>/Users/haleli/Desktop/MonkeyRunner/MonkeyRunner.py</string>
  </array>
  <!-- 运行间隔 -->
  <key>StartInterval</key>
  <integer>5</integer>
<!-- 标准输出文件 -->
<key>StandardOutPath</key>
<string>/Users/haleli/Desktop/MonkeyRunner/run.log</string>
<!-- 标准错误输出文件,错误日志 -->
<key>StandardErrorPath</key>
<string>/Users/haleli/Desktop/MonkeyRunner/run.err</string>
</dict>
</plist>
  • Label:全局唯一标识
  • ProgramArguments:第一个参数为要执行的命令或文件路径,其他参数为传入参数
  • StartInterval:设置执行的时间间隔,单位为秒
  • StandardOutPath:标准输出文件
  • StandardErrorPath:标准错误输出文件,错误日志

从这个简单的任务可以发现,其实plist文件就是一些列的键值对,赋予每个键相应的含义。更多的键值用法可以参考苹果官方文档Launchd-Apple Developer

3、launchctl 的一些命令

// 加载任务
$ launchctl load com.test.task.plist

// 删除任务
$ launchctl unload com.test.task.plist

// 查看任务列表, 使用 grep '任务部分名字' 过滤
$ launchctl list | grep 'com.test.task.plist'

// 开始
$ launchctl start com.test.task.plist

// 停止
$ launchctl stop com.test.task.plist

参考文章

1、https://www.jianshu.com/p/aebaa6a53ca5
2、https://blog.csdn.net/lgt633744/article/details/45932035
3、https://hanks.pub/2016/03/28/mac-launchctl/
4、https://www.cnblogs.com/hanlingzhi/p/6505967.html
5、https://cloud.tencent.com/developer/article/1327501
6、https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mac下,可以使用以下两种方法设置定时任务: 1. 使用cronjob cronjob是Mac OS中自带的任务管理器。可以通过以下步骤设置定时任务: 打开终端,输入以下命令: ``` crontab -e ``` 这将打开cronjob的编辑页面。在页面中添加你想要执行的命令及其执行时间。例如,如果你想每天晚上8点执行一个Python脚本,可以添加以下内容: ``` 0 20 * * * /usr/bin/python /path/to/your/script.py ``` 这将在每晚8点执行script.py文件。 2. 使用launchd launchd是Mac OS的系统级别的任务管理器。可以通过以下步骤设置定时任务: 创建一个plist文件,该文件包含你想要执行的命令及其执行时间。例如,以下是一个用于每天晚上8点执行Python脚本的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> <key>Label</key> <string>com.example.pythonjob</string> <key>ProgramArguments</key> <array> <string>/usr/bin/python</string> <string>/path/to/your/script.py</string> </array> <key>StartCalendarInterval</key> <dict> <key>Hour</key> <integer>20</integer> <key>Minute</key> <integer>0</integer> </dict> </dict> </plist> ``` 将该文件保存到~/Library/LaunchAgents目录中。 使用以下命令加载该任务: ``` launchctl load ~/Library/LaunchAgents/com.example.pythonjob.plist ``` 这将在每晚8点执行script.py文件。 无论使用哪种方法,都需要确保Python环境配置正确。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值