使用crontab定时执行PHP

2016-11-20  本文已影响0人  yundGo

首先创建一个php文件,用来执行你的任务
#vi /home/a.php

<?php
  $myfile =  fopen("/home/test.txt", "a") or die("Unable to open file!");
  $txt  =  date('Y-m-d H:i:s',time())."\n";
  fwrite($myfile, $txt);
  fclose($myfile);
?>

再创建一个test文本文件
#touch /home/test.txt

php 的准备工作就完成了,下面配置crontab

找到PHP的执行路径
#whereis php

Paste_Image.png

然后配置crontab
# crontab -e

Paste_Image.png
这里是每一分钟执行一次 /home/a.php
启动crontab
#service crond start
然后看下有没有想要的效果,查看test.txt 的内容 Paste_Image.png
上一篇 下一篇

猜你喜欢

热点阅读