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> |
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 ); } } |
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 -" ); } } |
http://www.codenterprise.com/how-to-create-cron-job-in-php-dynamically/
Comments
Post a Comment