Home » Php » Scheduling a task in PHP MVC Codeigniter

Scheduling a task in PHP MVC Codeigniter

Posted by: admin July 12, 2020 Leave a comment

Questions:

I want to schedule a task which is a controller method in MVC CodeIgniter. Also, I want to add the option in my web application for the admin to change the schedule instead of working around windows task scheduler manually. I found some examples for PHP Web Form scheuler but not for MVC CodeiIgniter! Could you please guide me in this regard?

If my question is not clear please let me know which part you need more clarification!

Many thanks in advance,

How to&Answers:

First of all scheduling a task is a OS base Operation, so you cannot control it through your application unless you have full rights to your server, however there are some ways where you can achieve this
Check this article it will help you about how CI controls the cron job.

If you want to change/create the time of the cron job through your application then you can use
exec function which will execute your passed command in the shell of windows but remember this function will only work when your hosting provider has enabled it or you have rights to this function and this answers will tell how you can use schedule task (CronJob) on Windows through command line.

Answer:

I am not sure if I understand your question, but to schedule a task if working on a linux server you can use a cron job.

Suppose you have a controller called Welcome with a function called task, then you can use a cron job similar to this:

Cron_configuration path_to_bin_php index.php welcome task

Here is a link with more details on cron:

http://www.thegeekstuff.com/2009/06/15-practical-crontab-examples

Answer:

Do this,

add this on config/routes.php

$route['my_batch'] = 'your_controller/function_name';

In your your_controller:

public function function_name(){
        your task....
        }

    }```
now go to c-panel and search for cron job.

1.select custom command
2. put this command "curl --silent http://your_domain.com/your_controller/function_name" (without quotes). 
3. select timings and save it.