51

I am using CodeIgniter for my website. I have to use cron job to run one of controller function. I am using route in website. And also I am not using index.php in URL.

e.g. http://example.com/welcome/show, here welcome is my controller and show is function name of that controller.

I have used like this,

0 * * * * php /home/username/public_html/welcome/show

It is giving 'No such directory'

How can I set cron jon in cPanel for above URL.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Sachin
  • 1,273
  • 5
  • 16
  • 27

15 Answers15

49

Use:

php index.php welcome show

as command in your crontab. E.g.:

0 * * * * php /home/username/index.php welcome show

Source (ver. 2.2.0) http://www.codeigniter.com/userguide2/general/cli.html

Source (ver. 3.*) http://www.codeigniter.com/user_guide/general/cli.html

Source (ver. 4.*) http://codeigniter.com/user_guide/cli/cli.html

Linga
  • 10,379
  • 10
  • 52
  • 104
unicorn80
  • 1,107
  • 2
  • 9
  • 15
19

I have used below cron

php /full-path-to-cron-file/cron.php /test/index

source: http://www.asim.pk/2009/05/14/creating-and-installing-crontabs-using-codeigniter/

This works for me.

Thanks to all

Sachin
  • 1,273
  • 5
  • 16
  • 27
13

You can try with this one:

wget api.example.com/index.php/controller/function
Suvash sarker
  • 3,140
  • 1
  • 18
  • 21
  • 3
    Thanks for reply, means the url like using my example is `0 * * * * wget http://my-domain.com/welcome/show`, is this right? – Sachin Oct 09 '13 at 10:44
  • I think so,you can try with this – Suvash sarker Oct 09 '13 at 10:46
  • Thanks Suvash.. This url work for me. wget yourSite.com/index.php/controller/method – Adnan Ahmad Feb 19 '17 at 05:54
  • 1
    i set 2 cron for every minute and these creats many tmp files . – Devil Nov 29 '17 at 06:20
  • 1
    **Be careful** if you are using it, because basically `wget` is downloading a website as a file, so your server will have so many files. For example, I run 2 times of `wget localhost/my_controller/auth`, then I check with `[root@myserver bin]# ls -l | grep auth` command, I have 2 files: `-rw-r--r-- 1 root root 57 Feb 27 07:53 auth` and `-rw-r--r-- 1 root root 57 Feb 27 07:53 auth.1` – HendraWD Feb 27 '18 at 07:58
  • This have a full negative impact on the server performance, it's much better use an a relative url at the cron jobs. – Fernando Torres Oct 15 '19 at 13:03
11

You can also try:

0 * * * * /usr/bin/curl --silent --compressed http://example.com/welcome/show

Or localhost

0 * * * * /usr/bin/curl --silent --compressed http://localhost/welcome/show

I hope that is helpful.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
alditis
  • 4,633
  • 3
  • 49
  • 76
4

/usr/local/bin/php /home/username/public_html/index.php controllername methodname

This worked for me.

Reshma D
  • 59
  • 3
  • 1
    thanks for this solution! just one quick question with this solution if i have a login in my app it would request why and how can i avoid it? @Reshma D – Cesar Mtz Mar 19 '16 at 16:39
2

Here is the cron I use

enter image description here

/usr/bin/php /home/pia/www/jobs/index.php cron newsletter

Explanation:

a) $_SERVER['DOCUMENT_ROOT'] = /home/pia/www

b) codeigniter website root = /home/pia/www/jobs

c) 'cron' = controller name

d) 'newsletter' = method name

Florin
  • 5,781
  • 2
  • 20
  • 30
1

I have done it as

 00 09-18 * * 1-5 /usr/bin/php /var/www/html/app/index.php crontest

crontest is the name of the controller which also uses a model to pull data from the database and send mail periodically (between 9 AM to 6 PM on Monday to Friday every week)

I just viewed this page which explains very detail with example. Hope this will be useful to others as well.

Rajesh
  • 934
  • 12
  • 23
  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Tunaki Jan 28 '16 at 10:04
1

I am using codeigniter 3.0.3 and my server is hostgator. For me, the below format is working fine

*/15 * * * * /opt/php55/bin/php /home/username/public_html/myapp/index.php reminders index

above command runs every 15 minutes, reminders in command is controller name and index is method name.

Draken
  • 3,134
  • 13
  • 34
  • 54
prudhvi259
  • 547
  • 2
  • 9
  • 31
1
watch -n60 curl [your application path]/check_banalce/user_balance

in my case im using codeigniter and the above command executes the user_balance function which is found in check_balance controller every 60 sec.

Devaniyas
  • 9
  • 5
1

On a Linux EC2 intance, this worked:

*/5 * * * * /usr/bin/php /var/www/html/cifolder/index.php [module] [function]
ssaltman
  • 3,623
  • 1
  • 18
  • 21
1

If you are using the Hostgator(or any other Linux server) then try this one.

/opt/cpanel/ea-php72/root/usr/bin/php /YOUR_HOME_DIRECTORY/YOUR_USERNAME/public_html/marketing/index.php welcome emailcampaign 1

for example for me its

/opt/cpanel/ea-php72/root/usr/bin/php /home3/adnan/public_html/index.php welcome emailcampaign 101

where

welcome is the controller name
emailcampaign is the function name of welcome controller
101 = First argument of url.  
Adnan Ahmad
  • 848
  • 1
  • 13
  • 12
0

Set up cron jobs through cPanel using this procedure:

  1. Log on to your cPanel Interface.
  2. Go to ''Advanced' section.
  3. Click on "Cron Jobs".
  4. Select the specific time from the lists provided.
  5. You should enter the command to run in the "Command" field.

enter image description here

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
0

If you are using cPanel then Use the following command:

/usr/bin/curl -k http://example.com/welcome/show

This works perfectly for me.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Bikram Pahi
  • 1,107
  • 1
  • 12
  • 33
0

I am using hostgator's cPanel.
I have created a user controller and run_cron_data function inside the user controller.
Command: wget www.example.com/index.php/user/run_cron_data

See the below screenshot

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Mohit Tiwari
  • 165
  • 2
  • 6
0
* * * * * php index.php controllername functionname

1st * - minute, 2nd * - hour, 3rd * - day of month, 4th * - month, 5th * - day of week.

For more info visit : https://crontab.guru/