CentOS通过Crontab定时任务执行PHP脚本
业务需求
创建Crontab定时任务,通过PHP脚本每分钟写入一条数据。
准备测试数据
准备一张测试的数据表test:
CREATE TABLE `test` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8创建测试脚本文件Test.php,该脚本不建议放在web能直接通过url访问到的目录下,安全性不高 :
[root@VM_0_14_centos home]# pwd
/home
[root@VM_0_14_centos home]# vim Test.php
Test.php内容:
<?php
$link = mysqli_connect("127.0.0.1", "root", "123456", "testdb");
if($link){
mysqli_query($link,'insert into test (`name`) values("test")');
}
mysqli_close($link);
?>如果脚本的命令不生效,可能是Test.php的权限不够,设置一下权限即可,如755:
[root@VM_0_14_centos home]# chmod 755 Test.php创建crontab定时任务
[root@VM_0_14_centos home]# crontab -e*/1 * * * * /www/server/php/72/bin/php /home/Test.php测试定时任务
查看crontab执行记录,如果有记录则说明执行成功
[root@VM_0_14_centos home]# tail -n 10 /var/log/cron
Apr 16 10:00:01 VM_0_14_centos CROND[25269]: (root) CMD (/www/server/php/72/bin/php /home/Test.php)3分钟后,如果脚本代码没报错,会有三条记录。
#CentOS
本文标题:CentOS通过Crontab定时任务执行PHP脚本
本文链接:https://www.befun.ink/detail/11.html
声明:本站信息原创或由互联网收集,未用于商业用途,如若侵权,请联系站长删除!
懒师傅敲代码
优秀作者 战斗力十足
1.9w
文章
312w+
阅读
635w+
访问量
相关文章