0

I want to create a cron job which will run with the user define time in database. For eg.User can set start time and end time in the database .When the end time is reached I want cron to trigger one script to send mail.First of all Is it possible in cron or I have to go with some different approach ? and this all things will be done on AWS EBS. Below is what I tried on my local machine to just send a simple mail which is too basic

*/1 * * * * /usr/bin/php -q/var/www/html/cronTry/cron.php
Arnab Nandy
  • 6,472
  • 5
  • 44
  • 50
Ankita.P
  • 442
  • 1
  • 8
  • 17
  • 1
    yes why not, fetch your database entries and pass them to shell script then your cron will run as per database stored values. – Arnab Nandy Feb 03 '15 at 07:22

2 Answers2

2

You dont want to run the CRON every minute instead create a specific job for your user and run it once. Something like this should work (untested - on mobile)

$your_users_date;
$cmd = "sudo crontab -l | { cat; echo ". date("i H d m",strtotime($your_users_date))." * php /path_to_your_script.php arg1 arg2; } | crontab -";
shell_exec($cmd);

This will append a job to the crontab at the time your user has defined

Mike Miller
  • 3,071
  • 3
  • 25
  • 32
  • No probs. You will probably need to tidy up the crontab at some stage to get rid of all the dead jobs (if not they will run again next year). Easiest solution would be to write them to a database and run another cron that checks for used jobs and removes them from the crontab – Mike Miller Feb 05 '15 at 18:42
1

Get the dynamic details from the database, create the command string and

You can easily run your cron by passing the command in the shell_exec()

Veerendra
  • 2,562
  • 2
  • 22
  • 39
  • I got till passing the command to shell_exec(), what I am not getting is as to the time at which I should run the cron because time is entered by user and that can be anything. If I run every seconf will it not be a load on server? – Ankita.P Feb 03 '15 at 08:27
  • @Ankita.P You can get the time entered by the user from the database. Either you can take the time value from the POST or you must be saving them in database – Veerendra Feb 03 '15 at 08:47
  • Once I reach home I will give it a try . thanks alot :) If I have any doubt will surely get back to you – Ankita.P Feb 03 '15 at 09:06
  • @Ankita.P Sure you can get back – Veerendra Feb 03 '15 at 09:15
  • I tried Mikes soln and it worked . Thanks alot for your help too. :) – Ankita.P Feb 05 '15 at 18:15