【转】
1、apt-get install php5-cli
安装使其 生成 /usr/bin/php php可执行程序 向php文件发送消息
2、编写要执行的php文件,在/var/www/test.php
内容如下:
#!/usr/bin/php -q
<?php
$file = "/var/www/".date('H').'_'.date('i').'_'.date('s').'.txt';
file_put_contents($file,date('Y-m-d'));
?>
然后 chmod +x test.php 赋予test.php 可执行的权限(最好设置文件权限为1777)
3、 命令 crontab -e
*/1 * * * * /var/www/test.php
意思为每分钟执行test.php
4、/etc/init.d/cron restart
第二种方式:
1、vi /etc/crontab
*/1 * * * * root /var/www/test.php
2、也可以去掉test.php 头部第一行#!/usr/bin/php -q
那么crontab任务写法为:
*/1 * * * * root /usr/bin/php /var/www/test.php
注:php执行的路径为相对/root 最后写上绝对路径