cronjob in codeigniter using cpanel
Asked Answered
M

5

7

My website hosting server is hostmonster.com.

My application uses codeigniter framework.

I have a code which sends emails to my users and I want to make it automatic.

I have used the cpanel of the hosting service and I tried to give the command as

php -q www.mysite.com/admin admin sendDailyEmail

my controller is admin and the method is sendDailyEmail and the controller is present inside the application/controllers/admin folder.

I have also set a reminder email to me whenever the cronjob is run.

The email subject reads

Cron php -q /home1/username/public_html/admin admin sendDailyEmail

and the body says

No input file specified

Where do I go wrong.

I have never run cronjobs and this is my first time. I am no good in giving command line instuctions too.

My admin sendDailyEmail code is as follows

function sendDailyEmail() {
    $data = $this->admin_model->getDailyData();
    foreach ($data as $u) {
    if($u->Daily){
     //if(!$u->Amount){
       if ($u->Email=='[email protected]') {

                $user['user_data']['FirstName'] = $u->FirstName;
                $user['user_data']['LastName'] = $u->LastName;
                $user['user_data']['Id']=$u->Id;

                $this->email->clear();
                $this->email->to($u->Email);
                $this->email->from('[email protected]', 'MySite');
                $this->email->subject("My Subject");
                $msg = $this->load->view('emails/daily_view', $user, true);
                $this->email->message($msg);
                if ($this->email->send())
                    $data['message'] = "Daily Emails has been sent successfully";
                else
                    $data['message'] = "Daily Emails Sending Failed";
            }
        }
    }
    $data['main_content']['next_view'] = 'admin_home_view';
    $this->load->view('includes/admin_template', $data);
}
Mythology answered 5/10, 2013 at 15:12 Comment(1)
I have removed the authentication but if i now copy paste the url, it just sends away an email to my users. I fear that if someone knows, they keep on sending emails just by using the url. How do I restrict this. I am using codeigniter. The cronjob is run through the cpanel. previously i have this code in my sendDailyEmail if (!$this->session->userdata('variable')) redirect('admin/admin', 'refresh');Mythology
T
16

You can use wget and set the time for whatever you like:

wget http://www.mysite.com/admin/sendDailyEmail

You can also use curl:

curl --silent http://www.mysite.com/admin/sendDailyEmail
Taconite answered 5/10, 2013 at 17:2 Comment(3)
The cronjob is done and I got an email saying ** --2013-10-05 11:20:01-- mysite.com/admin/admin/sendDailyEmail Resolving www.mysite.com... 66.147.240.184 Connecting to www.mysite.com|66.147.240.184|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 0 [text/html] Saving to: sendDailyEmail' 0K 0.00 =0s 2013-10-05 11:20:11 (0.00 B/s) - sendDailyEmail' saved [0/0] ** But it did not send out the emailMythology
If you type http://www.mysite.com/admin/sendDailyEmail directly in the browser does it send the email?Taconite
Tried this in my shared hosting env and got "Permission Denied" for wget and "request has been blocked by Mod Security" for curl.Farmstead
E
12

For CodeIgniter 2.2.0

You can try this:

 php-cli /home/username/public_html/index.php controller method

or at your case

 php-cli /home/username/public_html/index.php admin sendDailyEmail 

It works fine with me.. Cheers!

Ec answered 16/10, 2014 at 8:43 Comment(2)
i'm going to try ..feedback you after few hoursPapain
this helped me. for some reason, cpanel doesn't run scripts with php binary, but with php-cli.Chthonian
T
3

Codeigniter sets up command line differently for running crons, etc.

Read: http://www.codeigniter.com/user_guide/general/cli.html

So you should run:

php index.php admin admin sendDailyEmail

(that may need adjusted; based on your code above)

Have a look at an article I just wrote that goes a little deeper into it all:

http://codebyjeff.com/blog/2013/10/setting-environment-vars-for-codeigniter-commandline

Thorr answered 5/10, 2013 at 21:3 Comment(0)
O
1

Here's my example for the cron job on cpanel using Codeigniter 3 framework:

/usr/local/bin/php /home/user/public_html/project/index.php controller method

Credits to Shinerweb youtube channel https://www.youtube.com/watch?v=Pgc2vzrlwok

Opulence answered 11/3, 2023 at 14:10 Comment(0)
P
0

i have facing same issue while, but following work for me

wget http://www.yoursite.com/controller/function
Papain answered 5/10, 2013 at 15:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.