top
logo


Home LAMP Linux PHP Cronjob with parameters

PHP Cronjob with parameters

PDF Print E-mail
(5 votes, average 5.00 out of 5)
Written by Alex Balyuk   
Thursday, 05 August 2010 18:35

PHP the most popular scripting language in the world. Many people use it for different things. I recently worked on a custom joomla component that required a scheduled task (cronjob). The task required joomla libraries so I created a "cronjob" controller to handle this scenario. Controller gives access to all MVC features, such as models and therefore tables. If you know, Joomla has only one entry point and it is right through the front door (e.g. index.php) with parameters that route to desired destination. When I wanted to schedule my crontab I found out that it is not as simple as it seems. Here is what you have to do to create any cronjob that executes php page/script and required query string (key/value) parameters.

Briefly about cronjobs. To edit a cronjob you have to launch the terminal and to edit the crontab, by executing

crontab -e

Cronjob looks like this

* * * * * command
de

As you can see there are 5 stars. Keeping * in the cronjob will execute the cronjob ever minute/hour/day/month/dow depending on where the star is, there can be multiple stars. The stars represent different date parts in the following order:

  1. minute (from 0 to 59)
  2. hour (from 0 to 23)
  3. day of month (from 1 to 31)
  4. month (from 1 to 12)
  5. day of week (from 0 to 6) (0=Sunday)

If you want to execute a php page/script without parameters it is very simple, all you have to do is

* * * * * php /complete/path/to/php/file.php

However if you want to execute the cronjob with parameters you will face some problems. For example

* * * * * php /complete/path/to/php/file.php?secretApiKey=123456
will not work, php will not understand that you are giving it parameters, this is query string syntax. To make example above work we would have to execute it like this

* * * * * php /complete/path/to/php/file.php 123456

The down side is that we do not have key/value parameters and would have to pick them by index, e.g $_GET[0].

The work around is simple, instead on executing php command we will execute wgetwget. Executing wget will load the requested webpage, just like you do with your browser.

* * * * * wget "http:/localhost/path/to/php/file.php?secretApiKey=123456"

This will execute the page/script on our local server and have key/value parameter, you will be able to get the parameter by the string index e.g. $_GET['secretApiKey']. The example above will be downloading the page every time the cronjob is executed and you will end up with collection of files like this

  • file.php?secretApiKey=123456
  • file.php?secretApiKey=123456.1
  • file.php?secretApiKey=123456.2
  • file.php?secretApiKey=123456.3
  • ...

To fix this we have to add additional parameters to supress the output and handle the content differently. To supress the command output we will add -o /dev/null and to supress file download we will add -O /dev/null. /dev/null is device null or it other words nowhere, if you are interested you can learn more about /dev/nulllearn more about /dev/null.

* * * * * wget -o /dev/null -O /dev/null "http:/localhost/path/to/php/file.php?secretApiKey=123456&param1=value1&param2=value2"

If you want to secure you files, to make sure nobody else accesses them, change the file permissions and ownership. In linux terminal

chown YourUserName /path/to/file.php
chmod 700 /path/to/file.php

And that is how you execute php files or scripts cronjob style.

Last Updated on Thursday, 19 August 2010 15:33
 

Comments 

 
0 #1 Rilwis 2011-10-14 02:25
Really useful. I've searched for a solution for the URL parameter, but there aren't good answers in the Internet. Your explanation is cool.

Thanks for helping me.
Quote
 
 
0 #2 Aladin 2012-04-17 15:04
Thank you man! Simple and easy
Quote
 

Add comment


Security code
Refresh



Copyright © 2012 (bool) cast.com. All Rights Reserved.
Joomla!Joomla! is Free Software released under the GNU/GPL License.GNU/GPL License.

bottom
top
Site Map | Contact Us

bottom