为cron作业编写php脚本以执行函数的步骤
我需要实现每天早上8:00运行的cron作业。我不知道需要做什么。 cron应该在此文件上运行函数greet()为cron作业编写php脚本以执行函数的步骤,php,mysql,cron,Php,Mysql,Cron,我需要实现每天早上8:00运行的cron作业。我不知道需要做什么。 cron应该在此文件上运行函数greet() <?php class Person { public $age=0; public $isalive=false; public $name; public $msg; public $isAlive=true; public $firstname; public $lastname;
<?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;
?>
转到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();
其中file.php包含:
<?php
include ('Person.class.php');
$teacher = new Person('boring','12345',12345);
$student = new Person('Swapnil','Shende',24);
$teacher->greet();
$student->greet();
?>
“我需要实现一个cron作业,每天早上8:00运行。我不知道需要做什么。”-尝试阅读cron手册。但我需要编写php脚本来安排cron运行函数greet()这是cron作业的功能,它将为您安排它,只需编辑file.php,在末尾调用函数,如$student->greet();和$teacher->greet();谢谢,这提供了一个更好的主意。@rahulRaj欢迎大家欢呼并愉快地编码:)为了让它工作,您需要在文件的开头使用hashbang(
#!/usr/bin/php
),否则系统将不知道如何运行php文件。或者,您可以直接将其添加到cron行中(/usr/bin/php/path/to/script.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;