How to run a batch file in cakephp2 at a certain time? - php

I want to run a batch file automatically in cakephp2 at midnight everyday but I have no clue.
It would like to have some help.

With it being a batch file I assume you're referring to a microsoft server. Simply add a task in the task scheduler to have it run at midnight each night, as your would have a cron job complete the task in a unix system.

I have no experience with Cake php framework but i believe you want to use cron jobs to execute the task.
Cron Jobs can run at specific times.
Please refer to the below link for some info
http://www.adminschoice.com/crontab-quick-reference/
http://en.wikipedia.org/wiki/Cron

Related

How to write PHP cron jobs without cPanel?

I know we have cron jobs in PHP. but I have a project in development phase and we won't have cPanel access.
We have a PHP + MSSQL application that needs to check the database periodically every 1 minute and collect the data and send a mail to a store admininstrator.
How can we do this?
You can have a alternative of cron jobs solution by implement you function in a file (eg: /very/secret.php if your jobs need to be secure, make sure the function can be call only when it get the right parameter eg: /very/secret.php?key=long-random).
Then use some free cron job server on the web like: https://www.easycron.com/ or https://www.setcronjob.com/ (just do a web search for "free online cron jobs"). You give them your URL and some configuration and then your jobs will be executed by them at a specific time of day.
I understand that you need to run a script to check the database periodically every one minute and collect the data and send a mail to store admin. Also I understand that you have a script and you need to run that script every one minute.
It can be done using "cron job", if you are using Linux server. Or if you are using Windows, there is a way to schedule the task (to run that script every min).
Note: it is nothing to with cPanel. Actually, the cPanel provides a user friendly GUI to schedule the cron job.
But if you are not using panel, you can do it manually.
If you are using Linux, here you can see, how to add the cron job - http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
If you are using Windows, here you can see, how to add the cron job - http://windows.microsoft.com/en-us/windows/schedule-task#1TC=windows-7

Creating multiple cron jobs with PHP

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.

How to run the PHP script at scheduled time

I need to run a php script at the scheduled time daily to update some fields in database and to send automated email. How I can do this?
Is it possible to write some service in XAMP server to run the script daily at scheduled time? I have no idea how to update database and send email automatically at the scheduled time. Can any one share some ideas or concepts?
I am using PHP and MySQL running in Linux Server.
You should use a Cron job to do it. Check out the examples on the Wikipedia page.
The Cron Job should call a script using the php executable that runs the necessary task.
Just create the script that does the required job, test it by hitting the URL in your browser once you are sure it works right. Copy the URL and add a Cronjob
Then schedule this command to run at whatever time you want to run
php ABSOLUTE_URL_TO_SCRIPT >> logfile
The log file is optional. But it will give you a chance to see what happened.
For example if you want to run your script every 4 hours, and assuming your script is at http://localhost/work/scripty.php and assuming that your http root is /var/www,
you would run "crontab -e" in terminal and add the following line:
* */4 * * * php /var/www/work/scripty.php
If you need more information just comment I will update the answer.
PHP cannot run script by itself,since php doesn't have daemons like python !!
So you have to take OS help to invoke your custom script .
For example in linux :
(example.sh)
export USE_PHP=php
cd $SCRIPT_ROOTDIR
$USE_PHP -f cronfile.php service="checkdatabase"
(service is the parameter passed to your cronfile).
For setting up cron jobs ,have a look at this link
http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/
In Linux, We can create .sh file and can give a specific time to run that is called cron job.
SO should use this method just make a shell file and give a time period to it.
You should take a help with linux expert for that.
Use the following: Cron Job

Getting a php script to run at a certain time each day independently each day?

I am in need of something like this I need a script to run independently each day at a certain time without fail, the script will be for PDO mysql and some other things,
Can anyone point me in the right direction??
Thanks
If PHP is configured to run from the commandline you can simply setup either a cronjob (crontab -e) (Linux) or a scheduled task (Windows) where you run the php file(s) you want to run.
php -f /path/to/script.php
If you're on Linux, use a CRON Job. For Windows, use a Scheduled Task.
There are also services that do this online. A quick Google search returns this site.

How to assign cron jobs through php

Is it possible to schedule some tasks in php to run automatically on the server at a certain time of the day? . I heard abt cron jobs. is there any way to set it via php code
http://php.net/system lets you to run any command-line utility.
As for the certain command to set a cron job you have either to google a little or ask on serverfault.

Categories