0

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.

$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.

May I know is this the correct way to schedule a cronjob from php script?

I thought this is how we execute a terminal command from php script but I'm getting nowhere.Please someone help me out here. Thank you.

1

1 Answer 1

1

Assuming you want to schedule new cronjobs, this is not the way to go about it.

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). If so, it runs them and updates the last_execution_datetime to the time and date of the execution.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually my aim here is to schedule a cronjob dynamically right after an insert query with those time and date variables passing in to the command.
Yes, if I understand your code correctly you want to schedule the sending of an outgoing email. Is it a one time sending or is the same mail going out regularly? In the first case it can be made simpler. Create a column in the email table to keep track of whether the email has been sent or not. Create a script that checks if the email is past its scheduled sending time and if it has been sent. If it is not yet sent and the time is passed, then have the script execute your monthlyautomaticemail.php. Create a cronjob to run the first script as often as you'd like to check, maybe once a minute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.