November 2nd, 2011, 10:59 PM
-
Set start date and time for cron job using php
Can we schedule cron job at specific date and time using php
I know
#!/usr/bin/php -q
in start of php will help to start cron job
but can we schedule its start Date and time and 'sleeping time' by modifying this script
or any other way
I want user to type their New year message
Say " Happy new year 2011"
in a form and submit
which will store this message in database
When user clicks Submit
a newyear.php (already written and working OK) will start sending emails.
This program gets executed as cron job since
i have put
#!/usr/bin/php -q
in begining of newyear.php
However this will start the cron job immediately.
But user want to schedule
this cron job on 31st December 2011 at 9 pm.
and sleep after every 5 minutes
Also after sending all emails the cron job to be stopped.
Stop sending emails
Note
I am setting value of field 'email_sent' value to 1
after sending of an email
hence when cron job starts after 5 minutes
i can start sending email to membersfor whom the filed
email_sent is =0
November 3rd, 2011, 12:02 AM
-
You'd need to do this in your script. CRON jobs just run. to the best of my knowledge you can't schedule them to start at a particular time.
All you need to do is check the time before you do anything else.
Code:
if (mktime () > strtotime ('2011-12-09 21:00:00')) {
// Your code in here...
}
November 3rd, 2011, 04:11 AM
-
Originally Posted by vikaspa
But user want to schedule
this cron job on 31st December 2011 at 9 pm.
and sleep after every 5 minutes
You can specify a cronjob to run once only on 12/31 but it will do it every year. That should not be a big deal since you will have 1 full year to delete it from the crontab if you don't want it to run again on 12/31/12.
0 21 31 12 * /path/to/php/script
As for it sleeping every 5 minutes, that would have to be done in the script itself. Unless you are saying you want the cronjob to run every 5 minutes starting 12/31/11 until the end of the day or something. In that case Catacaustic's suggestion is what you would have to use.
November 3rd, 2011, 07:54 AM
-
thanks ....Need more clearance
as specified
#!/usr/bin/php -q will start the cron job immediately..
what will be the period in this case i.e. default
i mean this cron job will run
- every 1 minute
- every 1 hour
- every day
- only once ...
please clarify
as suggested by Catacaustic ,let the job run after every 5 minutes ... the condition whether to execute should be in program / script
like
if (mktime () > strtotime ('2011-12-09 21:00:00')) {
// Your code in here...
}
in short not to start cron job using php ?
November 3rd, 2011, 08:14 AM
-
I think one of us is not understanding the problem. You say, "#!/usr/bin/php -q will start the cron job immediately". That is not a crontab entry so that will not start anything. What you show appears to be the first line of your script that tells the shell to run the script using the php command. There is nothing here that has anything to do with cron.
Please explain exactly what you want to do. All I get is that you want to launch your PHP command at 9PM on 12/31. I have already explained how to do that. I'm not sure what you want beyond that except something is supposed to happen every 5 minutes.
November 3rd, 2011, 08:45 AM
-
cron
Originally Posted by gw1500se
I think one of us is not understanding the problem. You say, "#!/usr/bin/php -q will start the cron job immediately". That is not a crontab entry so that will not start anything. What you show appears to be the first line of your script that tells the shell to run the script using the php command. There is nothing here that has anything to do with cron.
Please explain exactly what you want to do. All I get is that you want to launch your PHP command at 9PM on 12/31. I have already explained how to do that. I'm not sure what you want beyond that except something is supposed to happen every 5 minutes.
Thanks.
as specified by you
#!/usr/bin/php -q
do not initiate the cron job .. i was under wrong assumption
best policy is to have cron job running and write script that will admatch the requirement
November 3rd, 2011, 09:08 AM
-
It is still not clear to me what you want to do or even if you still have a question. However, you might want to investigate the 'at' command. It can launch a program once at a specified date/time.
November 3rd, 2011, 09:19 AM
-
my requirement
Originally Posted by gw1500se
It is still not clear to me what you want to do or even if you still have a question. However, you might want to investigate the 'at' command. It can launch a program once at a specified date/time.
I wanted to start / stop cron job using php
without defining from cron tab using control panel
Also wanted user to specify start / end time for cron which is
i feel it was ideal condition ...
November 3rd, 2011, 09:47 AM
-
Oh! I see said the blind man as he picked up a hammer and saw.
That is simple enough. First you read the current crontab ('exec("crontab -l");') lines into your PHP script. Then you locate and change/replace the desired line (if there is only 1 line in your crontab you can skip this step completely). Once you have the new line(s) set up simply write back to crontab (via 'popen("crontab");').
November 3rd, 2011, 11:39 AM
-
Sounds to me like you want to have a cron task perform a specific job (such as send emails) but only if that job needs done (ex, a user added something to send) and at the time it's supposed to be done (ex, a user adds a send on date).
In a case like that, what you do is this:
1) When you user's submit the work request, you add it to a work queue with all the information required. For example, add an entry to a database table 'messages' with the message text, send date, etc.
2) Write a script that will process items from that work queue. Ex, write a script that will select all the items with a send date that is before the current date/time and then send out those items. Mark the items as being sent so they are not sent a second time.
3) Setup the script for processing the queue on a cron schedule that will cover the minimum time someone may enter as a delay. Ex, if they can only set the date, you only need to run it once a day. If they can set a date+hour, you may need to run it once an hour. If they can set an exact time, run it each minute.
Whenever your cron runs, if there is work to be done it will do it. If there is nothing, it does nothing.
There are of course other (maybe better) ways to do whatever you want, but we'd need more specific details to help further.
Recycle your old CD's
If I helped you out, show some love with some reputation, or tip with Bitcoins to
1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
November 3rd, 2011, 12:31 PM
-
thanks kicken - u have understood
Originally Posted by gw1500se
Oh! I see said the blind man as he picked up a hammer and saw.
That is simple enough. First you read the current crontab ('exec("crontab -l");') lines into your PHP script. Then you locate and change/replace the desired line (if there is only 1 line in your crontab you can skip this step completely). Once you have the new line(s) set up simply write back to crontab (via 'popen("crontab");').
Correct
i want user to define the task
by defining the task timing..
well ,
you have understood it correctly
a table will store date and time defined by user
cron tab will run task (a php script say newyear.php) which wil be sending emails.
The script will check date and time (as defined by user)
when the date and time is withing norms
it will execute the script and start sending email
or
i will be having member table with field email_sent
email will be sent to only those whose field 'email_sent'
is =0
when user submits date and time
newyear.php
on specified date and time
the field value email_sent will be set to zero
this will start process of sending email
once email is sent , value of field email_sent will be set to 1
which means email already sent
Actually i wanted the cron job to be started using php script
but it seems
we have to make provision in the script that gets executed
November 3rd, 2011, 12:39 PM
-
It seems we cannot start / stop cron job using php
It seems
we cannot start / stop jobs using php
November 3rd, 2011, 01:10 PM
-
Yes you can. A couple of ways to do that have already been suggested to do that. Why do you think you cannot?
November 3rd, 2011, 01:46 PM
-
May I ask the code
Originally Posted by gw1500se
Yes you can. A couple of ways to do that have already been suggested to do that. Why do you think you cannot?
May I Ask the PHP code to that will start and end the running cron job
November 3rd, 2011, 05:22 PM
-
First you have still not said what you are trying to do so I have to guess. Second, forget cron, the way you are replying indicates that cron has absolutely nothing to offer with respect to what you want to do. As I understand it, you simply want to start a background process from a PHP page and stop that process from a PHP page. Based on that, you start and stop the background process with 'exec' (the 'kill -SIGHUP $PID' command will stop it). That being said, it is up to the background process to handle the signal and do whatever it needs to do to terminate gracefully.