How to automate my cron php script - php

I'm using PHP and was wondering how can i run my PHP script to run at midnight and check if a member has been inactive for six months if so delete.

See Crontab: http://en.wikipedia.org/wiki/Cron
example crontab entry for 12:01 am:
1 0 * * * /usr/bin/php /home/whatever/myscript.php > /home/whatever/outlog.txt
command to edit the crontab entries for the logged in user:
crontab -e

A cron line like the following should suffice:
0 0 * * * /usr/bin/php /path/to/script
I would advise that you run the script at an irregular time (i.e not on an hour or day boundary), as you may get to the situation when you have too many scheduled tasks occurring at the same time.
If the script generates any output, then that will typically be emailed to the user mailbox. So, you should design your script to not generate output in the normal case. Or, alternatively redirect the output to a file or the syslog.
I use syslog redirection quite a lot. For example the following will log the commands output into the syslog using the tag my_script.
20 1 * * * /usr/bin/php /path/to/script | /usr/bin/logger -t my_script

Related

How to run cron job in php

I have php function that I wnat to run every 15 minutes. I found some questions like this:
How can I make a cron to execute a php script?
and in my case I should use this:
*/15 * * * * /usr/local/bin/php -q /path/to/my/file.p
But should I run this command in terminal or put it in my file? And once, it is executed, will it run all the time or will have time limit?
Thanks!
PHP doesn't run cron jobs, your server (or operating system) is doing this. There are two ways to put your cron job to work:
#1
Using the shell command crontab. The command crontab -l will list all existing cronjobs for your user (most likely there are none yet). crontab -e will open an editor window where you can put in a you cron job as a new line. Save, and your cron job is now running. crontab -l again and you will see it listet. crontab -r to remove all the cont jobs.
You can also start a cron job from a file. Simply type crontab filename (eg. crontab textfile.txt)
Alternatively you can also start it from within PHP. Just put your cron job into a file and start it via exec() like so:
file_put_contents( 'textfile.txt', '*/15 * * * * /usr/local/bin/php -q /path/to/my/file.php' );
exec( 'crontab textfile.txt' );
#2
If you have admin privileged on your system you can create a file in /etc/cron.d/ (for example, call it my_cronjob) and put your cron job there. In this case you probably want to run it as a user (not as admin, that would be rather insecure). This is quite easy to do, just add the user name like so:
*/15 * * * * user_name /usr/local/bin/php -q /path/to/my/file.p
(In this case the cron job will not be listet under crontab -l)
Answering your second question: As long as the cron job is listet in crontab -l or as long as the file is sitting in /etc/cron.d the cron job is running, in your case, every 15 minutes.
10 * * * * /usr/bin/php /www/virtual/username/cron.php > /dev/null 2>&1
There are two main parts:
The first part is "10 * * * *". This is where we schedule the timer.
The rest of the line is the command as it would run from the command line.
The command itself in this example has three parts:
"/usr/bin/php". PHP scripts usually are not executable by themselves. Therefore we need to run it through the PHP parser.
"/www/virtual/username/cron.php". This is just the path to the script.
"> /dev/null 2>&1". This part is handling the output of the script. More on this later.
Please read this tutorial http://code.tutsplus.com/tutorials/scheduling-tasks-with-cron-jobs--net-8800

Codeigniter CLI command with Tsohost not working

I currently have a PHP website built with codeigniter, and i'm having issues with CLI and cron jobs.
The CLI is setup so the controller running the script is found in the /application/controllers/scrape on the server (looking via the ftp) this would be /public_html/application/controllers/scrape, the function to run is called all_sites.
I'm hosted with TSOhost and can successfully run the command using the browser via URL (website.com/index.php/scrape/all_sites)however the script times out, hence the need to use a cron job to run the script.
So far i have tried the following raw cron commands in the advanced mode in the TSOhost control panel when trying to get the script to run daily:
The TSOhost technician set this up
03 19 * * * /usr/bin/php-5.3 /var/sites/s/website.com/public_html/application/controllers/scrape.php (didn't work)
0 6 * * * /usr/bin/wget -O /dev/null -o /dev/null http://www.speeddatemate.com/index.php/scrape/all_sites
0 6 * * * /usr/bin/php-5.3 /var/sites/s/speeddatemate.com/public_html/application/controllers/scrape/all_sites
03 19 * * * /usr/bin/php-5.3 /var/sites/s/speeddatemate.com/public_html/application/controllers/scrape.php
03 19 * * * /usr/bin/php-5.3 /var/sites/s/speeddatemate.com/public_html/application/controllers/scrape/all_sites
10 18 * * * /usr/bin/php-5.3 /var/sites/s/speeddatemate.com/public_html/index.php scrape all_sites
TSO host have stated:
For referencing your site path, use /var/sites/s/website.com
The path to PHP 5.2 is /usr/bin/php and for 5.3 it's /usr/bin/php-5.3
The technician also said:
"To run from CLI you would need to find a way to get those parameters into the script, they can't be appended to the command."
Although is this not the Point of the command?
I've also tried running it via the "make a http request" option which creates the raw job as:
0 6 * * * /usr/bin/wget -O /dev/null -o /dev/null http://www.speeddatemate.com/index.php/scrape/all_sites
Again this does not work.
I've searched high and low to find a way to get this working and read various posts and tried various methods nothing has worked. Can anyone help?
First off, don't bother setting up a cron job unless you have it working on the command line. You will end up mixing different things and generating a plethora of unwanted possibilities for debugging which will confuse you further.
Now, you are saying that
I can successfully run the command using the browser via URL (website.com/index.php/scrape/all_sites), however the script times out
This could be because the script takes longer than 30 seconds to complete, and thus is hitting the max-execution-time in php. Check this.
If this is the case, check this and answers over here and here to resolve it by increasing the limit.
Once you have a working version of the script, either via browser or via command line, you can go ahead and schedule the cron jobs, like already shared by your TSOhost tech support.
03 19 * * * /usr/bin/php-5.3 /var/sites/s/website.com/public_html/application/controllers/scrape.php (didn't work)
0 6 * * * /usr/bin/wget -O /dev/null -o /dev/null http://www.speeddatemate.com/index.php/scrape/all_sites
Once again, setup the crons only after you have the other pieces working.
If it still doesn't work, give more info regarding:
What exactly the script does? Does it download something, does it backup something; update the question with whatever it does.
What parameters does it require? and how were you passing them via the url?
What do the logs say? Assuming your webserver is apache, you can usually find them at /var/log/apache2/error.log and /var/log/apache2/access.log.
EDIT 1
OP says in comments that php index.php scrape all_sites works for him.
Assuming that works from the root of of his app, where path to index.php can be asssumed to be /var/sites/s/website.com/public_html/application/index.php, try this cron job then
03 19 * * * cd /var/sites/s/website.com/public_html/application/ && /usr/bin/php-5.3 index.php scrape all_sites
If possible, schedule it for a time closer to current time rather than a fixed job for 19:03
If this still doesn't work, and assuming the max-execution-time has already been taken care of, the problem could be with one of your environment variables - your cli shell environment is having some variables that are missing from your cron environment.
In my experience, I have found that PATH variable causes the most troubles, so run echo $PATH on your shell, and if the path value you get is /some/path:/some/other/path:/more/path/values, run your cron job like
03 19 * * * export PATH="/some/path:/some/other/path:/more/path/values" && cd /var/sites/s/website.com/public_html/application/ && /usr/bin/php-5.3 index.php scrape all_sites
If this doesn't work out, check all the environment variables next.
Use printenv > ~/shell_environment.txt from a normal shell to get all the environment variables set in the shell. Then use the following cron entry * * * * * printenv > ~/cron_environment.txt to get the variables from the cron environment.
Compare the two - shell_environment.txt and cron_environment.txt for any unset variable which you need to tinker with in cron environment.
First of all make sure your script run as expected from your browser. If so then you can try to run it from command line. Lets assume following is your controller.
<?php
class Scrape extends CI_Controller {
public function all_sites($some_parameter = 'working')
{
echo "Its {$some_parameter}!".PHP_EOL;
}
}
?>
As per your provided information to run the command you can run
/usr/bin/php-5.3 /var/sites/s/website.com/public_html/index.php scrape all_sites
And set cron as
03 19 * * * /usr/bin/php-5.3 /var/sites/s/website.com/public_html/index.php scrape all_sites
If you need to pass a parameter, pass the parameter like:
/usr/bin/php-5.3 /var/sites/s/website.com/public_html/index.php scrape all_sites "Working fine"
Enjoy!
You can read reference document about Running via the CLI from codeigniter here

How can I set Crontab for a specific hour?

I'd like to set my Cron job to work at specific hour.
Particularly, I'd like to set it at 1PM and 7PM every day of the year. How can I do?
I wrote two line like those below:
0 13 * * * /usr/bin/php path/myphp.php
0 19 * * * /usr/bin/php path/myphp.php
but nothing works fine! Can someone help me?
0 13,19 * * * /usr/bin/php path/myphp.php should work, check your log / user mail for errors.
Keep in mind that there's a difference in format between a user's crontab (accessed with the command contab -e or what have you) and the system's crontab, managed in files like /etc/cron.d and others.
In a user's personal crontab, the format you used should work.
In the system crontab, (if you place a new file or edit anything under /etc), make sure you specify the user name to run as before the command, for example:
0 13,19 * * * www-data /usr/bin/php path/myphp.php
Will run the command /usr/bin/php path/myphp.php as user www-data every day at 13:00 and 19:00.

Best ways in which to conduct background tasks

Using PHP, I want to have scheduled tasks based upon the time the server is currently running.
Say at 7pm on Sunday I want a database query to be ran.
The way in which I've considered doing this is to put the task in the script that is ran on each page load in the session init.
Any ideas?
One method to automatically run a PHP script at specified time intervals is to use Crontab. This can be particularly beneficial for scripts that need to automatically update information without user interaction such as a script that gathers website statistics so that they can be emailed to you or a script that regularly retrieves content from another website.
See: PHP CLI and Cron
You can just use cron to trigger your script for you with something like this:
If you have ssh access you can add a crontab entry like this:
crontab -e
and enter something like this:
0 19 * * 0 php -f /path/to/script/file.php
where 19 is the hour (7pm), 0th day of the week (sunday), and 0 is the minute.
this will run at 7pm on sunday.
This is what your cron tab is for. You can specify whatever tasks, scripts, or other programs you need to run at whatever time you want to run them. Cron is run by your operating system and goes by your operating system's clock.
For example if I wanted to run a PHP script every minute, I would put something like the following into my cron tab.
* * * * * php /path/to/script.php
How you actually create cron entries depends on what your server setup is like. If you have some sort of shell access on a Unix/Linux system, you can edit your cron tab easily by running the following command.
crontab -e
That will bring up the crontab in the default text editor.
If you are using something like cPanel, you will have to consult the manual for information on how to edit cron entries.
The actual syntax for the cron can be complicated at first. There are entry generators online that you can use if you need help.
0 * * * * php /some/script.php # This will execute at the 0 minute of every hour.
0 1 * * * php /some/script.php # This will execute at the 0 minute of the first hour of every day. In other words, every day at 1AM.
You should use your systems Cron deamon to schedule and run a PHP file.
First read up on Cron:
https://help.ubuntu.com/community/CronHowto
Then implement this:
Open crontab:
vim crontab -e
Add an entry to your cron table:
0 19 * * 0 /path/to/php /path/to/script.php
Your script.php will contain the code/logic to query the database.

How do I use cron to run a database cleaning script?

I have a database with a bunch of links that I want to keep updated. Basically if a link returns a 404 error code I want to remove it from the database. I have a script that I am using however I need to run it manually. How can I make this work using CRON?
in your shell as cron user (or root):
crontab -e
This will bring up your crontab file in your editor. Add a new line something like this:
* */12 * * * /path/to/script
Save/exit the file.
Now for a quick lesson on cronjobs:
-The first 5 arguments in the line tell how often, or when the cron daemon will execute the 6th argument.
-From left-right, arguments represent: minutes, hours, days, weeks, months
-An asterix (*) tells the cron to run on all values of it's associated time measurement (example * * * * * means to run every minute, of every hour, of every week, and of every month!)
In my example, * */12 * * * means to run every 12 hours.
Check out: http://kevin.vanzonneveld.net/techblog/article/schedule_tasks_on_linux_using_crontab/
To run a PHP script with cron you can use the PHP executable and the path to the script.
On most linux systems you want to edit your cron file (the crontab) with the command crontab -e. This will open up a command line based editor and you can just append your new job to the bottom of the file using this format.
<minute> <hour> <day_of_month> <month> <day_of_week> php /path/to/script
If the commands dont work for you let me know what distribution you are using and I can modify the instructions.
/usr/bin/php -q /home/user/public_html/script.php

Categories