Heim  >  Artikel  >  Betrieb und Instandhaltung  >  Geplante Aufgaben zur Konfiguration der MAC-Umgebung

Geplante Aufgaben zur Konfiguration der MAC-Umgebung

演明
演明Original
2021-08-26 15:51:502137Durchsuche

我想配置个定时任务,就是到某个时间执行代码里的一个文件,或是每隔多长时间执行,开始有了这样想法,但是不知道怎么执行,后来上网查了很多资料,下面是我配置成功的,我整理了一份笔记供供大家参考。

相关推荐:                     
1.《MAC环境安装php、apache、MacPorts等环境配置
2.《MAC环境下安装node、vue等环境配置
3.《MAC环境下Laravel的安装及配置

开启crontab服务

    (1)、用下面命令检查需要的文件是否存在,页面显示 ls: /etc/crontab: No such file or directory 表示crontab文件不存在。

$  ls -al /etc/crontab

Geplante Aufgaben zur Konfiguration der MAC-Umgebung

(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

Geplante Aufgaben zur Konfiguration der MAC-Umgebung

页面提示/var/db/locate.database) does not exist 表示不存在,需要创建一下

$ sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist

(9)查看配置

$ cat /System/Library/LaunchDaemons/com.vix.cron.plist

Geplante Aufgaben zur Konfiguration der MAC-Umgebung

(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的文件的配置

<?php
require_once dirname(dirname(__FILE__)).DIRECTORY_SEPARATOR.&#39;application&#39;
.DIRECTORY_SEPARATOR.&#39;app&#39;
.DIRECTORY_SEPARATOR.&#39;batch&#39;
.DIRECTORY_SEPARATOR.&#39;Controller.php&#39;;
Batch_Controller::main_CLI(&#39;Batch_Controller&#39;, &#39;batch_lili_yinbiao&#39;);

(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.&#39;application&#39;
.DIRECTORY_SEPARATOR.&#39;app&#39;
.DIRECTORY_SEPARATOR.&#39;batch&#39;
.DIRECTORY_SEPARATOR.&#39;Controller.php&#39;;
Batch_Controller::main_CLI(&#39;Batch_Controller&#39;, &#39;batch_lili_yinbiao&#39;);

(4)、batch_lili_yinbiao.php业务逻辑文件「这里不做说明」

以上是我配置的定时任务,可以根据自己的需要执行项目里的文件。如果大家想配置按照上面步骤一步步操作就好,如果哪有不懂可以留言。谢谢大家支持。希望能为小白提供帮助。

Das obige ist der detaillierte Inhalt vonGeplante Aufgaben zur Konfiguration der MAC-Umgebung. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn