Menu
Latest Articles
- Make Apache listen on a different port
- XAMPP fails to start Apache 2 on Windows
- Remove Powered By text from RSMonials
- Joomla Zip Archiver
- Installing XDebug on XAMPP
- Joomla .htaccess - redirect non-www to www
- Remove Powered By text from JDownloads
- Joomla Custom PayPal IPN
- Using multiple databases within Joomla Framework
- Ajax Control Toolkit AsyncFileUpload - The file attached is invalid
PHP Cronjob with parameters |
![]() |
![]() |
![]() |
| 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 Cronjob looks like this deAs 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:
If you want to execute a php page/script without parameters it is very simple, all you have to do is However if you want to execute the cronjob with parameters you will face some problems. For example 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 thisThe 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 wget 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
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/null If you want to secure you files, to make sure nobody else accesses them, change the file permissions and ownership. In linux terminal And that is how you execute php files or scripts cronjob style. |
| Last Updated on Thursday, 19 August 2010 15:33 |
is Free Software released under the GNU/GPL License.





Comments
Thanks for helping me.