Is it possible to create and schedule a task from within a php app?
I want to give my users the option to schedule and run the script at a time of their choosing without them getting into cron jobs or windows task scheduler.
You could give your users access to a "cron" table that mimics the options that cron offers. There they can store the jobs the need to execute.
Then you can run a cron job every minute that checks that table to see if there are entries / jobs that need to be processed.
Related
Currently, I have done the following:
I created one scheduled task which runs daily to get the Scheduled time from Mysql DB for the currentdate and store it into the .txt file
SELECT workflow_id, DATE_FORMAT(schedule_datetime,'%H:%i')TIMEONLY FROM scheduling_event
where DATE(schedule_datetime) = CURDATE()
Created one more scheduled task that runs each 5mins to check if the scheduled time present in the .txt file matches the CURRENT TIME if yes then it calls the scheduled_program.php file.
The issue here is - this is not an efficient way if nothing is scheduled on the current date. So Is there any way to create/update a dynamic scheduled task instead of running each 5mins? ie: the first scheduled task will run and take the scheduled time on the current date then it will create a task based on the scheduled time. if the day ends delete all the scheduled tasks for the day.
Note: Number of the scheduled task is not fixed. imusing Windows 10, php7.
I am trying to achieve, run a scheduled_program php file on schedule Date and TIME
It looks like you're trying to solve a problem that's not serious. I guess you don't want to waste your computer's time running a cron job every five minutes if it has nothing to do.
But here's the thing:
a cron job
that runs a php program
that does a single query
to retrieve a list of workflows
and run them
has negligible cost if you run the cron job every five minutes, or even every single minute, and there are no workflows to run.
On the other hand, debugging and troubleshooting cronjobs is hard, especially in production.
So, I respectfully suggest you keep this system as simple as you possibly can. You will have to explain it over the telephone to somebody in the middle of the night at least once. That's the unfortunate truth of scheduled tasks. The dynamic scheduled task system you propose is not simple.
I have a website that users create their own tasks to be run at a specific time.
At the moment, when the user clicks create task it will add to the database table 'tasks'.
I then have a cron job, which runs every 15mins. It gets all the entries in the table and checks first whether it has been run today using the date time. (every midnight, another cron runs through and resets all these values). If it has not ran this day, it will then run it and afterwards change the value to ran.
The cron job does this by running php file on my server that checks for tasks, and then uses a simple foreach statement.
At the moment, there are only 11 users and my server seems fine. I am wondering what will happen with lets say 10,000 users.
Is this an efficient way to handle thousands of cron jobs? How yould you go about running this?
I am using Cakephp.
I want to run scheduled jobs.
Here's the situation:
User set time for some task on UI (say 1 week).
When the time is over, I want to execute some specific task. Meanwhile, the user can also change the time, and the task should be executed at updated time.
What is the best way (and reliable) to achieve this?
PS: Complexity is not the issue. But the task must always run after specific time under all circumstances.
Set the execution date in your table in a field
Set a status (pending) as well
Run a cron job that runs a CakePHP shell every X seconds or minutes, whatever you need OR create a shell that keeps running all time and check the records every X seconds in a loop.
The shell will process tasks that are configure for an execution date lower than the current date
Set the status to success or failed depending on the outcome
It's up to you how you want to handle failed tasks and if it's OK if a task executes 10secs later than configured or 10 minutes. There are multiple factors that play into this: Your interval of the cron job / query against the table. Do they have to processed in parallel? Is it OK to process them after each other? Your information is to vague.
The way to do that in CakePHP is to create a shell and run it with Cronjobs.
I'm in need of a way to create dynamic, one-off cron jobs to execute tasks at different times. Ideally, I would like to achieve this using PHP, so that when a user completes a certain action, the cron job is created and scheduled for a time that is calculated based on the time that the user completes said action. At any one time, there could be multiple cron jobs scheduled at once for different times. These cron jobs also need to be deleted upon completion.
I have tried searching around for something appropriate, however haven't encountered anything that works as I need. If anybody could point me in the right direction, that would be greatly appreciated.
Cheers
A possible solution:
Setting a Cron job that calls to CronJobManager.php every second.
Just make a regular daemon that calls for the CronJobManager.php.
Create a cronjob table in your database
The cron job table should contain these basic fields: path (to php file), run_time(datetime), last run (datetime) and type (like suicidal, if as you explain you want some cron jobs to delete themselves)
Connect CronJobManager.php with the cronjob table
Every time CronJobManager.php runs (that is, every second), loads the cron jobs. Then, comparing "now"'s time with each cron job's run_time you'll get which cron jobs to run.
For example, if cron job "foo" run_time is set to 18/04/2014 22:02:01, CronJobManager will run it when reaching that moment.
Notice that if Cron jobs executing time needs a lot of time, they'll get delayed and eventually a second or two will get lost.
Now, for every cron job that needs an execution, you would execute the related php file of that cron job, indicated in the path.
This is a general idea, and of course you would have to extend it with for example cron job states (idle, running, stop, etc).
In order to delete cron jobs you would implement this feature in the cron job object.
That is: the Cron Job class, once it has executed what it had to do, it would check its type (as defined in database). If it is 'suicidal', then it would delete the database row.
UPDATE
I updated the answer but I want to note something. If what you need is several cron jobs to run at once, in a specific second with 0 delay, then you need a cron job per task out of php that runs a specific file.
To achieve this functionality, you need to have a daemon that is running all the time that checks for these dynamic jobs and launches them. The setup is a tad complicated to put together, but if you are ready for such an endeavor, you can use the project PHP Resque Scheduler.
https://github.com/chrisboulton/php-resque-scheduler
You start up a daemon that runs all the time and then you can add jobs to a dynamic queue to be executed at any specified time in the future. I think you will find this suitable to everything you are looking to do.
I want to build an application that enables users to schedule emails to send any time. simply, write email message and schedule it so that the server sends it at time specified. I am using zend framework. How to do it in php? Can it be done with cron jobs? If yes, then what are the disadvantages of using cron?
can it be done with cron jobs?
Cron is a time-based job scheduler in Unix-like computer operating systems. The name cron comes from the word "chronos", Greek for "time".1 Cron enables users to schedule jobs (commands or shell scripts) to run periodically at certain times or dates. It is commonly used to automate system maintenance or administration, though its general-purpose nature means that it can be used for other purposes, such as connecting to the Internet and downloading email.
http://ubuntuforums.org/showthread.php?t=586478
I would run a cron job every minute and check if there are any mails ready to be scheduled. The quote from the forum topic below instructs how to run cron every minute.
crontab -e
then set a tab like
* * * * * /command
The first star is the minute section,
so having a star there will execute
every minute
In case it makes it more clear if you
wanted every 5 mins then it would be
*/5 * * * * /command/to/execute
And the other stars are from left to
right
minute hour dayofmonth month
dayofweek*
*0=sunday
Disadvantages of cron?
if yes, then what are the
disadvantages of using cron
When doing a lot of cronjobs you will have to spawn a lot of processes(pay cost of spawning process which is expensive). In that case it would be better to have background process(es) running continually and fetch messages from message queue. But when you want to run a cronjob only ever minute than I assume this will not be a big case.
I would tackle this using a cron job.
Simply create a script that checks for messages to send at a certain time. The user schedules for say 1PM (using a database of course), the script runs every 5 min, or so, and it checks (the db), are there any messages to go out for the current time? If so, it sends out the emails, else it sleeps.
Clean and simple way of handling it.
Disadvantages?
I can't see any, this is what a cron is made for, running tasks at specific times.