简体   繁体   English

如何在php脚本中编写和执行cron job命令?

[英]How to write and execute cron job command in php script?

I'm trying to execute a cronjob command from the script for this I'm writing below code, but nothing is happening.Can someone please help me out here. 我正在尝试从脚本中执行cronjob命令,这是我在下面的代码中编写的,但是没有任何反应。有人可以在这里帮助我。

$query ="INSERT INTO master_schedule_email(date, time,active_status) VALUES ('$date','$time','1')";
$exec = mysql_query($query) or die ("Error in Query".mysql_error());

    $minute = date ('i' , strtotime($time)); 
    $hour = date ('H' , strtotime($time));
    $date = date ('d' , strtotime($date));
    $month = date ('m' , strtotime($date));

$croncommand = "$minute $hour $date $month * monthlyautomaticmail.php";
exec($croncommand);

Here, I want to run the cron job command at the exact time and date which is inserting to the db table. 在这里,我想在插入数据库表的确切时间和日期运行cron job命令。

May I know is this the correct way to schedule a cronjob from php script? 我可以知道这是从php脚本安排cronjob的正确方法吗?

I thought this is how we execute a terminal command from php script but I'm getting nowhere.Please someone help me out here. 我以为这是我们从php脚本执行终端命令的方式,但我无处可去。请有人在这里帮助我。 Thank you. 谢谢。

Assuming you want to schedule new cronjobs, this is not the way to go about it. 假设您要安排新的cronjob,这不是解决问题的方法。

I suggest creating a database table for scheduled jobs with a date field for when they should be executed and a date field for when they were last executed. 我建议为计划的作业创建一个数据库表,并带有一个日期字段和一个日期字段,这些日期字段应该何时执行,而日期字段则最后一次执行。 Then you manually create a cronjob to run a script that checks the new table to see if there are any jobs that are past their due date (scheduled_datetime > current_datetime && last_execution_datetime < scheduled_datetime). 然后,您手动创建一个cronjob来运行一个脚本,该脚本检查新表以查看是否有任何作业超出其到期日期(scheduled_datetime> current_datetime && last_execution_datetime <Scheduleddatetime)。 If so, it runs them and updates the last_execution_datetime to the time and date of the execution. 如果是这样,它将运行它们,并将last_execution_datetime更新为执行的时间和日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM