为cron作业编写php脚本以执行函数的步骤

时间:2013-05-15 13:03:01

标签: php mysql cron

我需要实现一个每天早上8点运行的cron作业。我不知道需要做什么。 cron应该在这个文件上运行一个函数greet()。

<?php
    class Person {
     public $age=0;
     public  $isalive=false;
     public $name;
     public $msg;
     public $isAlive=true;
     public $firstname;
     public $lastname;

     public function __construct($fname,$lname,$age){
        $this->firstname=$fname;
        $this->lastname=$lname;
        $this->age=$age;
        $this->name=$fname." ".$lname;
        //$this->isAlive=$isAlive;
     }

         public function greet(){
             echo "$this->name says $this->msg my age is $this->age <br> am I alive:$this->isAlive";
         }
    }
     $teacher = new Person('boring','12345',12345);
     $student = new Person('Swapnil','Shende',24);
     echo $student->age;

     ?>

2 个答案:

答案 0 :(得分:1)

转到cron选项卡并按此打开

crontab -e

然后添加此行

* 8 * * *   filename.php

其中filename.php是您的文件名

还要编辑你的file.php,在最后调用这样的函数

 $teacher = new Person('boring','12345',12345);
 $student = new Person('Swapnil','Shende',24);
 $student->greet();
 $teacher->greet();

答案 1 :(得分:0)

crontab -e
* 8 * * *  file.php

其中file.php包含:

<?php
include ('Person.class.php');
$teacher = new Person('boring','12345',12345);
$student = new Person('Swapnil','Shende',24);
$teacher->greet();
$student->greet();
?>

并从您的班级中删除此部分

$teacher = new Person('boring','12345',12345);
$student = new Person('Swapnil','Shende',24);
echo $student->age;
相关问题