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...

Sublime vs Dreamweaver

13 REASONS TO USE OF SUBLIME TEXT OVER DREAMWEAVER 17 MARCH, 2014 BY TOM ELLIOTT I started writing this post over a year ago but back then it was entitled something along the lines of ‘Reasons why I use Dreamweaver over code editors’. This was biased, as I hadn’t properly explored other code editors like Coda, Sublime Text or Notepad++. So, wanting to write an objective post from a web developers point of view, and because I had heard many good things about it, I setup Sublime Text 2 and dove straight in. I’m glad I did because it quickly became obvious that my presumed reasons for Dreamweaver’s superiority were just wrong. We are all creatures of habit to a greater or lesser degree and when we develop workflows to help make our lives easier, we can get entrenched in the view that these honed practices are superior. It’s not our fault, confirmation bias is supposedly  programmed in our DNA . Even so, it’s healthy to try and keep questioning and challenging ourselves, espe...

JazzCash Mobile Account

  JazzCash Mobile Account Help Center  > JazzCash Mobile Account What is JazzCash mobile account? JazzCash Mobile Account is an actual bank account that is tagged with your mobile number and can be operated through your phone. Through this Mobile Account you can enjoy complete freedom of accessing financial services anywhere, anytime! More importantly, you don’t have to rely on traveling to a Bank branch, wait at queues or complete any documentation. Mobile Account menu works on all types of mobile phones – smart phone is not required. Customers can make deposits or withdrawals through any Mobilink Microfinance Bank Branch, Mobilink Franchise, Mobilink Business Center and Jazzcash Agents spread across Pakistan. JazzCash Mobile App In line with the continuous digitization of its services to meet demands of growing number of smartphone users, JazzCash is proud to announce Android based App for its Mobile Account users. The App offers a user friendly inte...