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

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

Difference Between Primary Key and Unique Key In Sql Server

Both  PRIMARY KEY  and  UNIQUE KEY  enforces the Uniqueness of the values  (i.e. avoids duplicate values) on the column[s] on which it is defined.  Also these key’s can Uniquely identify each row in database table. Below table lists out the major  difference between PRIMARY KEY and UNIQUE KEY : PRIMARY KEY UNIQUE KEY NULL It doesn’t allow Null values. Because of this we refer PRIMARY KEY = UNIQUE KEY + Not Null CONSTRAINT Allows Null value. But only one Null value. INDEX By default it adds a clustered index By default it adds a UNIQUE non-clustered index LIMIT A table can have only one PRIMARY KEY Column[s] A table can have more than one UNIQUE Key Column[s] CREATE SYNTAX Below is the sample example for defining a single column as a PRIMARY KEY column while creating a table: CREATE TABLE  dbo.Customer ( Id  IN...

Remove the Loadblanks.ru Browser Hijacker (Removal Guide)

What is Loadblanks.ru Browser Hijacker? Skip this and learn how to remove Loadblanks.ru Browser Hijacker! The  Loadblanks.ru Browser Hijacker  is a computer infection from the  Adware/ShortcutHijacker  family that changes the shortcuts for your installed web browsers so that they automatically open the loadblanks.ru web page. It does this by pointing the browser shortcuts to batch files located in the  %UserProfile%\AppData\Roaming\Browsers\  folder as shown below. These batch files contain an obfuscated command that opens the specified browser to a specific page. In this case, the browser will be forced to automatically open the http://ic.loadblanks.ru/ or http://im.loadblanks.ru homepages. An example of the command executed by the batch file is  start "" "c:\PROGRA~2\google\chrome\APPLIC~1\chrome.exe" "http://im.loadblanks.ru/c/b26cdff542ee7ff6?" . You can see an example of this obfuscated batch file below. Due to these modified sho...