我主要做的是一个员工生日当天发短信的功能,每天跑一次脚本,
第一步:
a.App/模块/ 下创建command文件夹
b.我这边是创建在admin模块里面,在command文件夹下创建一个SendMessage.php文件(具体名字自己根据需求定)
c.复制下面的代码到SendMessage.php
<?php namespace app\admin\command; use think\console\Command; use think\console\Input; use think\console\Output; use think\Db; use think\Log; class SendMessage extends Command { protected function configure(){ $this->setName('SendMessage')->setDescription("计划任务 SendMessage"); } //调用SendMessage 这个类时,会自动运行execute方法 protected function execute(Input $input, Output $output){ $output->writeln('Date Crontab job start...'); /*** 这里写计划任务列表集 START ***/ $this->birthday();//发短信 /*** 这里写计划任务列表集 END ***/ $output->writeln('Date Crontab job end...'); } //获取当天生日的员工 发短信 public function birthday() { echo '这里写你要实现的逻辑代码'; } }
第二步:在APP/command.php里面加上
return ['app\admin\command\SendMessage'];
第三步:设置crontab计划任务
-
crontab -l //计划任务列表
-
crontab -e //编辑新增
-
crontab -r //删除
为了方便测试,可以先设置成每分钟执行一次 ,记录一下日志/www/wwwroot/tool/runtime/message/2019.log
-
*/1 * * * * php /www/wwwroot/tool/think SendMessage>>/www/wwwroot/tool/runtime/message/2019.log 2>&1
-
//监控一下你的脚本是不是正常的
-
tAIl -f /www/wwwroot/tool/runtime/message/2019.log
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
评论(0)