当前位置: 移动技术网 > 网络运营>服务器>Linux > CentOS下采用Crontab实现PHP脚本定时任务

CentOS下采用Crontab实现PHP脚本定时任务

2019年06月01日  | 移动技术网网络运营  | 我要评论

简单实现一个需求,每5分钟往特定表中插入2条数据。经过分析还是采用crontab方式靠谱,另外执行php脚本的方式,不用担心链接超时等问题。

1、准备工作,创建1个数据表“person”,

create table `person` (

 `firstname` varchar(100) not null,

 `lastname` varchar(100) default null,

 `age` varchar(100) default null,

 primary key (`firstname`)

) engine=innodb default charset=latin1; 

2、创建1个php脚本“test.php”,实现往数据库表中插入的操作,这个脚本可以放到任何位置。ps:建议不放到wwwroot根目录下,因为这样的话,用户可以通过url就可以进行访问了,这样的安全不高,但是有一些方便之处,我们使用自带的一些php框架,例如ci、tp等,也可以使用自己封装的一些业务通用类!综合考虑实际情况进行决定。

<?php

$con = mysql_connect("localhost","root","idodopass01!");

if(mysql_select_db("test",$con))

{

mysql_query("insert into person (firstname, lastname, age) values ('peter','griffin','35')");

mysql_query("insert into person (firstname, lastname, age) values ('glenn','quagmire','33')");

echo "chenggong";

}

mysql_close($con);

 

echo "string";

?> 

3、创建crontab任务

crontab -e
*/5 * * * * /usr/local/php/bin/php /home/wwwroot/default/test.php

关于crontab命令参考链接:

4、其他

查看运行日志的路径:/var/log

 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网