使用Thinkphp6.0 在Linux搭建简易的计划任务

使用Thinkphp6.0 在Linux搭建简易的计划任务

实现流程
第一步:自定义指令
第二步:编写sh文件
第三步:Linux执行crontab -e

实现流程
1.创建一个自定义指令。
2.编写sh文件,随意放置,并给执行权限。
3.Linux执行crontab -e 编辑计划任务

第一步:自定义指令
1.进入项目根目录cmd,执行

php think make:command Contrab

输出:Command:app\command\Contrab created successfully.就说明自动的生成了文件

2.进入appcommandContrab修改execute方法

`protected function execute(Input $input, Output $output)

{
    //app\index\controller(命名空间) Index::index(Index控制器下的index方法)
    //注意要静态方法  
    call_user_func('app\index\controller\Index::index');
}`

3.配置config/console.php文件
`

return [
    'commands' => [
        'contrab' => 'app\command\Contrab',
    ]
];`

4.测试是否添加成功
`php think

找到Available commands,看下面是否出现了你新添加的contrab,出现则添加成功!

` build Build Application Dirs
clear Clear runtime file
contrab
help Displays help for a command
list Lists commands
run PHP Built-in Server for ThinkPHP
start this is a crawl tool!
version show thinkphp framework version``

5.测试是否调用成功
`php think contrab`

输出:'hello world!' 则表示调用成功` `
因为我在Index/index 输出的是'hello world!'

PS:Thinkphp6自定义命令行的具体文档地址:`https://www.kancloud.cn/manua...`
第二步:编写sh文件
1.新建后缀为sh文件
2.用编辑器去打开编辑,我用的是Sublime text 3
`#!/bin/bash
/usr/local/php7/bin/php /var/www/tp6/think contrab

说明:
/usr/local/php7/bin/php php执行文件路径
/var/www/tp6/think 项目根目录加上think
contrab 上面创建的命令名称`

3.保存到任意位置
我是放在/var/www/sh目录下,记住这个位置,等会有用!

4.给文件执行权限
chmod u+x /var/www/sh/crond.sh
第三步:Linux执行crontab -e
1.编辑计划任务
crontab -e

`输入i进入编辑模式
`添加 */1 * * * * /var/www/sh/crond.sh
按esc 输入:wq保存退出``

2.然后重启crond:
service crond restart
3.完成
现实了每一分钟输出了一次 hello world!
当然!具体业务需要各位自己编写。
到了这里很多朋友想深入学习swoole和laravel、thinkphp,swoft微服务在使用中遇到很多困难,为了大家都能够早日进阶PHP架构师,可以参考下这份PHP架构师路线图,请戳这里

你可能感兴趣的:(php)