mac使用launchctl定时运行程序

在linux下可以用crontab来定时执行任务,在mac下可以用launchctl来定时执行任务 

我们使用launchctl来做一个定时执行任务的例子 

首先做一个可执行的脚本,脚本名字叫做: 
run123.sh,脚本的功能就是在/Users/alecyan/Downloads/目录下建一个文件,脚本要改成可执行的权限 
chmod 777 run123.sh 
脚本代码如下: 
Java代码   收藏代码
  1. cd /Users/alecyan/Downloads/  
  2. touch abcabc123.txt  

然后进入到~/Library/LaunchAgents下建一个plist文件,这个就是糸统执行任务时要使用的文件 
文件名叫com.alecyan.testcron.plist 
文件内容如下: 
Java代码   收藏代码
  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.   <key>Label</key>  
  6.   <string>com.alecyan.testcron</string>  
  7.   <key>ProgramArguments</key>  
  8.   <array>  
  9.     <string>/Users/alecyan/Downloads/run123.sh</string>  
  10.   </array>  
  11.   <key>StartCalendarInterval</key>  
  12.   <dict>  
  13.         <key>Minute</key>  
  14.         <integer>4</integer>  
  15.         <key>Hour</key>  
  16.         <integer>13</integer>  
  17.   </dict>  
  18.   <key>StandardOutPath</key>  
  19. <string>/Users/alecyan/Downloads/abc.log</string>  
  20. <key>StandardErrorPath</key>  
  21. <string>/Users/alecyan/Downloads/abcerror.log</string>  
  22. </dict>  
  23. </plist>  
简单的对这里边的内容说明一下,label这里就是给这个任务名个名字,这里一般取plist的文件名,这个名字不能和其它的plist重复。run123.sh就是我们要执行的脚本,StartCalendarInterval里边的参数是说每一天13点4分的时候执行一下脚本。下面脚本表示每86400秒运行一次脚本 

Java代码   收藏代码
  1.   </array>  
  2.   <key>StartInterval</key>
  3.   <dict>  
  4. <integer>86400</integer> 
  5.   </dict>  


然后就可以用下面的几个命令进行操作我们做好的任务了 
Java代码   收藏代码
  1. launchctl load com.alecyan.testcron.plist  
  2. launchctl unload com.alecyan.testcron.plist  
  3. launchctl start com.alecyan.testcron.plist  
  4. launchctl stop com.alecyan.testcron.plist  
  5. launchctl list  

要加载我们做好的plist文件,就是用上面的第一个命令load然,这个时候糸统就会在每天的13点4分执行我们的脚本 
如果想去掉我们的定时任务就可以用unload命令 
如果一个任务今天的13点4分执行过了,然后你改了,com.alecyan.testcron.plist里面的时间,比如说改到14点4分执行,必须unload之后再重新load一下,不然当天不会再执行这个命令 
start可以测试任务,这个是立即执行,不管时间到了没有 
stop可以停止任务 
ok一个简单的定时任务就可以用了 

深入的再说一下,其实,/Library/LaunchAgents这样的目录在mac下一般有三个,我们上面说的是当前用户的目录下的,还有两个一个在/Library/LaunchAgents另一个在/System/Library/LaunchAgents/ 如果是不管哪一个用户都要定时执行的话,就要放在 
/Library/LaunchAgents这个下面 

参考博客: 
http://blog.jeffean.net/blog/2010/08/30/mac-os-shi-yong-launchd-kong-zhi-daemon/  
http://blog.hebine.com/archives/tag/launchd  
http://nathangrigg.net/2012/07/schedule-jobs-using-launchd/  
http://www.zhoumingzhi.com/2013/04/05/macos%E7%9A%84launchd%E4%BD%BF%E7%94%A8/  

https://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man5/launchd.plist.5.html这里是plist里的参数的具体说明


转自:http://ylq365.iteye.com/blog/1878917

### 使用 `launchd` 设置定时任务 对于希望在 Mac 上设置定时任务来定期运行 Python 脚本的情况,推荐使用内置工具 `launchd` 来实现自动化调度。此方法无需安装额外软件,并能处理异步代码,在计算机唤醒时自动执行预定的任务[^2]。 #### 创建 Shell 脚本来调用 Python 程序 为了简化管理并确保环境配置正确无误,建议先编写一个简单的 shell 脚本用于启动 Python 应用程序: ```bash #!/bin/bash /usr/bin/python3 /Users/demo/helloworld.py ``` 保存上述内容至文件名为 `run.sh` 的脚本中,并赋予其可执行权限: ```bash chmod +x ~/path/to/run.sh ``` #### 配置 `launchd` 定时器 接下来需创建一个新的 plist 文件定义何时以及如何触发该任务。可以将如下 XML 内容复制到 `/Library/LaunchDaemons/com.example.helloworld.plist` 中(注意调整路径和时间间隔): ```xml <?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.helloworld</string> <!-- 设定工作目录 --> <key>WorkingDirectory</key> <string>/Users/demo</string> <!-- 执行命令 --> <key>ProgramArguments</key> <array> <string>/usr/local/bin/python3</string> <string>/Users/demo/helloworld.py</string> </array> <!-- 日程安排:每天晚上十点 --> <key>StartCalendarInterval</key> <dict> <key>Hour</key><integer>22</integer> <key>Minute</key><integer>0</integer> </dict> <!-- 运行失败重试策略 --> <key>RunAtLoad</key> <true/> <!-- 是否保持常驻进程 --> <key>KeepAlive</key> <false/> <!-- 组名仅适用于root用户下 --> <key>GroupName</key> <string>wheel</string> </dict> </plist> ``` 完成编辑后加载新服务: ```bash sudo launchctl load -w /Library/LaunchDaemons/com.example.helloworld.plist ``` 以上操作即完成了通过 `launchd` 实现每晚固定时运行特定 Python 脚本的功能设定[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值