How to assign cron jobs through php - 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.

Related

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

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

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

How to handle a large number of Cron jobs

My webserver is running cpanel and has a Cron job module in it providing a GUI to add/remove/edit cron jobs.
Problem: I need to create a large number of cron jobs (more than 100). Each cronjob simply does a wget <url> to trigger a bunch of PHP functions. What will be the best way to manage them? The current problem with cPanel's Cron GUI is that there will be no organization in the cron jobs. If I have 5 related cron jobs, added seperately admist 100 other unrelated cron jobs, it will get harder to find these cron jobs in the long list of cron jobs.
Should I stick to cPanel's GUI? Or is there an alternative GUI with better features like folders or the ability to move cron jobs up and down the list? Or should I just have 1 cron job, which calls a PHP file that does the various wget in PHP using CURL, with a table and stores the jobs in a MySQL database, essentially creating my own version of cron that runs off a single cronjob which triggers every minute?
Try grouping them.
Say you have 10 groups of ten, depending on how you organise them.
Each one of the cron jobs calls a PHP script that calls 10 other PHP scripts.
If you need them run at different times you could do a simple check with time() to see which scripts should be called.
You could call them the other PHP scripts using cURL.
Make sure to set a large script timeout though, or you might end up with lots of things going wrong.
Could you create one job in CPanel that will run one script.
Then in that script you could create the logic to run the 100 scripts.
If they are to be run at different times then you could use switch(time), or similar to manage execution.

How can I make my PHP script run at a certain time everyday? [duplicate]

This question already has answers here:
PHP: running scheduled jobs (cron jobs)
(13 answers)
Closed 5 years ago.
I have a PHP script that checks my database's code for validity. How can I have this run everyday at a certain time so then I can just have it run on autopilot.
What you want is called a cron job and is already covered here:
PHP: running scheduled jobs (cron jobs)
If you are using cPanel on your host, you can go to Advanced --> cron jobs.
Then inside there you can set a cron job command like this:
wget http://yoursite.com/yourfile.php
which http://yoursite.com/yourfile.php should point to your desired function.
You can also set repetition time interval over there.
This way yourfile.php is called in specified intervals.
CronJobs feature are often disabled on most free or cheap Linux hosting. If you could use cron you could use that.
Alternatives:
Use services like : http://www.setcronjob.com/ (many exist, just search on google) , to schedule your task for free(1x per hour).
If you need a little bit more juice you should look into google app engine cron. In the cron you just do a simple asynchronous request to call your page.
There's a cli interpreter called php. Run your script through that as a cron job.
If you are a linux/unix user, i recommend you use CRON .
May it will helps you.
You can schedule a task in Windows - control panel->administrative tools is where you find task scheduler.
run a php automatically after every hour
Thanks
If you have your own server you can run a cronjob(unix/linux) or a scheduled task(windows).
If your app/site is hosted on a shared host that does not offer cronjobs you an use a sevice like
http://www.onlinecronjobs.com/
I have not tested this one but If you google a bit(fee conjobs) you will find plenty of those.
They will call a certain a url accoding to a fixed schedule. Most of those sevices have a fixed set of ip addeses so you can pevent the scipt from being called by anyone else.
You can easily use your database to save the last execution of the script.
Each execution of a certain page you run a script that does the following :
Check if more than 8 hours (example)
Checks in the database if the script has already been executed
If executed: does nothing.
If not executed: starts and updates the date last execution in database.

What options are there for executing a PHP script at a certain time every day?

I have a PHP script that needs to be run at certain times every weekday. Is cron or Windows task scheduler the only way to do this?
Is there a way to set this up from within another PHP script?
Depends how exact the timing needs to be. A technique I've seen used (dubbed "poor man's cron") is to have a frequently accessed script (a site's home page, for example) fire off a task on the first page load after a certain time (kept track of in the database).
If you need any sort of guaranteed accuracy, though, cron or a Windows scheduled task is really the best option. If your host doesn't support them, it's time to get a better one.
Apart from cron or Scheduled Tasks, you can have your PHP script to always run it. The process should sleep (within a loop) until the time has reached. Then, the process could execute the remaining functions/methods. Or, you can set up another script (which will act as a daemon) to check for the time and execute the other script.
Well since the web is a pull mechanism you have to have some sort of action that will trigger a PHP script to execute. cron is an option on *nix and task scheduler on windows. You could also write your own service that has a timer but only if needed, this is common on windows services for updaters, jobs etc.
One way you could do it is in the cron task just call a php script for each action needed. Or one php script that executes other tasks. The problem with web based tasks though such as PHP is timeouts. Make sure your tasks are under 60-90 seconds. If not you might look at using python , perl or ruby or even bash scripts to do the work rather than the PHP script.
cron seems like the best option for you though. You will have to call your script with wget. There are examples here: http://www.thesitewizard.com/general/set-cron-job.shtml
For instance this runs the script everyday at 11:
30 11 * * * /usr/bin/wget http://www.example.com/cron.php
Cron, of course, is by far the best way to schedule anything on *nix.
If this is in a remote server you do not have cron access to, you can setup cron/windows scheduler on your computer, to open a web browser to the page that contains the script you wish to run
You probably want to use cron (or windows scheduled tasks).
If you really wanted, you could set up another php script to run continuously with an infinite loop (with a sleep command inside the loop, say for 30 seconds or so) and then when you reach your desired day/time execute the other script via a shell command call. While possible, I can't think if a single good reason to use this method rather than cron/scheduled tasks
You can write a long running script that runs your main script in predefined times but it will be very unnecessary, error prone, and it will basically be a "cron rewrite in phph".
Using the real cron itself will be easier and a more robust solution. If you are packaging an application, you can put a file in /etc/cron.d which contains a single cron line running your application.
You'll need to use a cron job (under Linux/Unix) or a scheduled task under Windows. You could have another script running on a continuous basis which checks the time and executes a script at a specified interval, but using the OS-supplied mechanism is easier to manage and resilient to restarts, etc.
The Uniform Server project has some good suggestions on mimicking cron in environments where cron is unacceptable. Still though, if cron is at all an option, use it.

Categories