Skip to main content

CRON JOB in PHP dynamically


Prerequisites: Little knowledge of MVC framework and obviously PHP. Also safe mode should be off on server and PHP function shell_exec should be enabled.

I will describe how to create CRON job through PHP code within the MVC framework as simple as possible so that beginners and most programmers can easily understand. I want to describe in easy and short words as most programmers don’t feel very easy when they see a big length of an article and difficult technical wordings.
I would like to name mine
controller as cronController.php ,
model as cronModel.php and
view as cronView.php

Flow of creating cron job will go like this
cronController.php=>cronView.php=>cronController.php=>cronModel.php
Parameters from cronView.php will go to cronController.php and from cronController.php parameters will go to cronModel.php to make database operations. Here one thing should be noticed that its not necessary that you have to make model, but yes if you want to make database operations it will be good to use model. Here I will write (creating cron job script) in model because I need database insertion operation. You can also skip model if you want and write cron job script in controller.
cronView.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<form action=" cronController/cron_job_save" method="post" name="adminForm" id="adminForm" onsubmit="return check(this)">
 <table border="0" class="tablehead" width="100%">
 <tr>
 <th width="25%" align="left">Set Timings</th>
 <th width="75%" align="left"></th>
 </tr>
 <tr>
 <td align="left">Min</td>
 <td align="left"><input class="text_area" type="text" name="min" id="min" />
 </td>
 </tr>
 <tr>
 <td align="left">Hours</td>
 <td align="left"><input class="text_area" type="text" name="hour" id="hour" />
 (0-23)</td>
 </tr>
 <tr>
 <td align="left">Day of Month</td>
 <td align="left"><input type="text" name="day_of_month" id="day_of_month" />
 (1-31)</td>
 </tr>
 <tr>
 <td align="left">Month</td>
 <td align="left"><input type="text" name="month" id="month" />
 (1-12)</td>
 </tr>
 <tr>
 <td align="left">Day of Week</td>
 <td align="left"><input type="text" name="day_of_week" id="day_of_week" />
 (0-6)</td>
 </tr>
 <tr>
 <td align="left"></td>
 <td align="left"><input name="Save" type="submit" class="button" id="Save" value="Create" /></td>
 </tr>
 </table>
</form>
In the action of above form we wrote controllerName/ControllerFunction so it will go in cronController.php and will call cron_job_save function of cronController class and there do its action.
cronController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Class cronController{
 
    function index()
    {
    displayView("cronView.php");//displayView is function which will show view cronView.php
    }
 
    public function cron_job_save()
    {
        $objCron=new cronModel ();//cron is model name
        $objCron->create_cron_job($_POST);
 
    }
}
In above controller main function is cron_job_save which will be called after we submit form. All the values will be posted through $_POST to the model. Its optional to use model we can do our work in our controller also without going to model but I used model because I needed some database operations.
cronModel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class cronModel {
 
    function create_cron_job($args)
    {
        //Do database operation if you need to do //
        //Else write following code directly in controller
        $min            =$args["min"];
        $hour           =$args["hour"];
        $day_of_month   =$args["day_of_month"];
        $month          =$args["month"];
        $day_of_week    =$args["day_of_week"];
 
$filePath= /usr/bin/php /var/www/vhosts/mydomain.com/httpdocs/filename.php;
        shell_exec("(crontab -l ; echo "".$min." ".$hour." ".$day_of_month." ".$month." ".$day_of_week." ".$filePath." ") | crontab -");
    }
}
In above model we are performing necessary database operations and then after that calling shell_exec function which is taking day of month, month ,day of week and hour and minute at which they are supposed to execute and that’s it our cron job is set and filename.php will be executed at selected day, month, hour and minute.

http://www.codenterprise.com/how-to-create-cron-job-in-php-dynamically/

Comments

Popular posts from this blog

October CMS E-Commerce Tutorial: GoT White Walkers Protection Store

As many of you probably know, Game of Thrones Season 6 is starting next April 24th. Now I recently introduced myself on this blog , but forgot to mention that I'm, like millions of others, a shameless GoT fan . When our content guy asked me to craft a post showing how the easy e-commerce integration we brag about would work with October CMS, I immediately picked GoT as a theme for the demo. ​ So in this post, I'm going to show you how to set up a store selling defense against the imminent White Walkers invasion. Because WINTER IS COMING big time, you know. ​ ​ More specifically, I'll provide a step-by-step e-commerce tutorial explaining how to integrate our shopping cart platform to an October CMS site. Let's get into it. ​ What is October CMS ​ We've been hearing about October CMS from developers here and there for a while now. This free, open-source CMS platform is the brainchild of fellow Canadian Alexey Bobkov and Australian Samuel Georges. It...

Creating a Custom Page in OpenCart

Creating a Custom Page in OpenCart by MarketInSG » Tue Apr 17, 2012 6:59 am I noticed a lot of users are searching for help on this topic, so here's a short tutorial for all of you. What you need: - Basic PHP knowledge - Basic HTML knowledge - Text Editor Required files to create a custom page: - Controller file - Template file Optional files: - Model file - Language file (I will be using it in this example here) Controller file Create a file name 'static.php' in /catalog/controller/information/ code inside the file (php) <?php class ControllerInformationStatic extends Controller {    private $error = array();            public function index() {       $this->language->load('information/static'); //Optional. This calls for your language file        $this->document->setTitle($this->language->get('heading_title')); //Optional. Set the title of your web page.   ...

Paypal In Pakistan Payoneer-Verified

Since there are a huge number of online organizations which just acknowledge Paypal as installment passage. Thusly I am going to demonstrate to you how you can make Paypal record in Pakistan and in what manner would you be able to check it ! It is an unlawful trap however you can utilize it for crisis purposes. Instructions to make and confirm Paypal record in Pakistan  Simply take after the regulated guide underneath to get your Verified Paypal Account. You will additionally get Virtual Bank Account in USA and  1 Master  Debit Card free of charge You will have the capacity to utilize that check card to cashout your trusts through Paypal from any ATM Machine. Go to  Paypal,  Sign up and Choose your nation as United States of America. Use  Fake USA Address,  But other all data ought to be 100% right. When you information exchange, confirm your email and login to your Paypal record. Presently you have your Paypal record, now is the rig...