cronjob in codeigniter using cpanel

2019-04-07 14:02发布

问题:

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=='myemail@gmail.com') {

                $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('alerts@mysite.com', '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);
}

回答1:

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


回答2:

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!



回答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



回答4:

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

wget http://www.yoursite.com/controller/function