苹果Mac环境如何配置定时任务?
时间:2022-03-22 02:54:33
我想配置个定时任务,就是到某个时间执行代码里的一个文件,或是每隔多长时间执行,开始有了这样想法,但是不知道怎么执行,后来上网查了很多资料,下面是小编配置成功的方法供大家参考。
开启crontab服务
1.用下面命令检查需要的文件是否存在,页面显示 ls: /etc/crontab: No such file or directory 表示crontab文件不存在。
$ ls -al /etc/crontab
2.创建crontab文件。
$ sudo touch /etc/crontab
3.查看 crontab 是否启动。
$ sudo launchctl list | grep cron
-0com.vix.cron #否
320650com.vix.cron #是
4.确保本地现有一个被执行的文件,我在本地新建一个test.txt文件。
路径: /Users/gaopeng/Downloads/test.txt
$ cd Downloads #放到Downloads目录下 $ touch test.txt
查看一下这个文件的权限,如果不可读,不可写,还要开一下权限。
$ sudo chmod 777 test.txt
5.编辑自动执行脚本内容,此内容是临时内容,关机后不保存。
$ sudo crontab -e
6.每1分钟输出 1234 到脚本(注意使用绝对路径)。
*/1 * * * * echo 1234 >> /Users/gaopeng/Downloads/test.txt
7.使用crontab执行脚本,如果没有执行,查看脚本是否有执行权限(可忽略)。
$ sudo chmod 777 crontab
8.查看一下启动项的配置。
$ locate com.vix.cron
9.查看配置。
$ cat /System/Library/LaunchDaemons/com.vix.cron.plist
10.crontab服务的重启关闭,开启。
$ sudo /usr/sbin/cron start
cron: cron already running, pid: 32065 #页面显示
$ sudo /usr/sbin/cron restart
cron: cron already running, pid: 32065 #页面显示
$ sudo /usr/sbin/cron stop
以下示例是在具体项目里配置的定时任务。
一、php形式配置
1.编辑要执行的内容
*/5 * * * * /opt/local/bin/php56 /Users/mac/git/ssos_web/batch/batch_os_sll.php 2>&1
2.被调用batch_os_sll.php的文件的配置。
require_once dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.'application'
.DIRECTORY_SEPARATOR.'app'
.DIRECTORY_SEPARATOR.'batch'
.DIRECTORY_SEPARATOR.'Controller.php';
Batch_Controller::mAIn_CLI('Batch_Controller', 'batch_lili_yinbiao');
3.batch_lili_yinbiao.php业务逻辑文件「这里不做说明」。
二、sh 形式的配置:
1.编辑要执行的内容。
*/2 * * * * bash /Users/mac/git/ssos_web/batch/bin/batch_sh_os_all.sh
2.被调用batch_sh_os_all.sh的文件配置。
SCRIPT_DIR=`dirname $0` #取当前脚本所在路径的父目录
cd $SCRIPT_DIR#cd到返回的父目录
/usr/bin/php56 ../lcriptt/batch_os_sll.php $1 2>&1
3.被调用batch_os_sll.php的文件配置。
require_once dirname(dirname(dirname(__FILE__))).DIRECTORY_SEPARATOR.'application'
.DIRECTORY_SEPARATOR.'app'
.DIRECTORY_SEPARATOR.'batch'
.DIRECTORY_SEPARATOR.'Controller.php';
Batch_Controller::mAIn_CLI('Batch_Controller', 'batch_lili_yinbiao');
4.batch_lili_yinbiao.php业务逻辑文件「这里不做说明」。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
怎样对word文档进行排版?
![](https://img.aspxhome.com/file/2023/6/18576_0s.jpg)
Word文档怎么制作勾画重点的效果
![](https://img.aspxhome.com/file/2023/0/32060_0s.png)
wps表格中怎么加竖线?
![](https://img.aspxhome.com/file/2023/8/a168488_0s.jpg)
iOS系统不再预装YouTube的原因是这个
答疑:Win10运行绝地求生提示缺少msvcp140.dll和vcruntime140.dll文件
![](https://img.aspxhome.com/file/2023/0/46830_0s.png)
手机怎么拍出好看的全景照片?全景照片拍摄技巧
excel灰色区域怎么取消?excel灰色区域取消方法
![](https://img.aspxhome.com/file/2023/6/39776_0s.jpg)
win10崩溃了怎么办?win10崩溃了处理教程
![](https://img.aspxhome.com/file/2023/1/48001_0s.png)
Word2010文档如何标记关键词?
![](https://img.aspxhome.com/file/2023/6/18426_0s.jpg)
WPS文件怎么使用八爪鱼调整段落?
![](https://img.aspxhome.com/file/2023/4/a166144_0s.jpg )
如何利用WPS表格制作小型网址导航?
![](https://img.aspxhome.com/file/2023/3/a166963_0s.png)
保存在电脑上的word文件突然找不到怎么办?
![](https://img.aspxhome.com/file/2023/3/18643_0s.jpg)
win10磁盘占用率100%如何解决?win10磁盘占用率100%解决教程
![](https://img.aspxhome.com/file/2023/6/47656_0s.jpg)
Excel 单元格的引用方法
怎么删除wps文档的漫游文档
![](https://img.aspxhome.com/file/2023/3/a163893_0s.png)
EXCEL表中#N/A是什么意思?#N/A是什么?
excel中如何筛选出生日是某月某日
Word怎么查看历史文档记录
![](https://img.aspxhome.com/file/2023/1/31941_0s.png)
Excel怎么输入超过15位的数?Excel显示超过15位的方法
![](https://img.aspxhome.com/file/2023/8/a143668_0s.jpg)