1.在代码里面写调度任务,在Console文件夹下面Kernel.php写调取任务命令
2.在Commands文件夹下面创建相应的任务文件
3.在本地上面跑一下看看代码是否走通,在项目目录下运行
php artisan schedule:run 1>> storage/logs/null 2>&1
storage/logs/null表示在storage/logs下面创建null任务文件
4.测试通过,部署到服务器上,
在命令行输入
crontab -e
再输入
* * * * * /usr/local/php/bin/php /home/wwwroot/cron.test/cron/artisan schedule:run 1>> /home/wwwroot/cron.test/cron/storage/logs/null 2>&1
解释:/usr/local/php/bin/php 表示我的php安装目录(如果你的php配置的全局,可以直接php)
/home/wwwroot/cron.test/cron/artisan artisan能运行的位置(在项目目录下直接操作可以直接 artisan)
5.在命令行中运行:/sbin/service crond reload //重新载入配置
在命令行中运行/sbin/service crond restart //重启服务
正常运行(ok)
6.常见错误以及报错:
6.1查看storage\logs中的日志错误(建议先删除所有,vim是dG)
直接跑一下
/usr/local/php/bin/php /home/wwwroot/cron.test/cron/artisan schedule:run 1>> /home/wwwroot/cron.test/cron/storage/logs/null 2>&1
正常运行说明(命令代码都没错)crontab -e 中哪里有错,仔细看一下(我就是多写了个一个字母)
6.2如果报:The Process class relies on proc_open, which is not available on your PHP installation
解决方法:(先找到php的安装位置(which php),然后(find -name php.ini))
打开php.ini,并搜索disable_functions指令(/disable_functions),找到类似如下内容:
disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server
找到proc_open并删除即可。(注意重启一下)
然后再次运行
参考地址
```
https://www.oldboyedu.com/zuixin_wenzhang/index/id/279.html
```
网友评论