2

I am using codeigniter to create some cronjobs I scheduled it using Task schedular in windows 7. It runs every one minute but what i see is it just popup my code in an editor and does not insert any data in my database that I mentioned in index function.

<?php
class Hello extends CI_Controller {
 public function __construct() {
    parent::__construct();
    $this->load->database();
}

public function index() {
     $this->db->query("INSERT INTO test_cron VALUES(null, 'username')");
}
}
?> 

How can I really execute it so That it insert data in my database

Thanks

Faryal Khan
  • 881
  • 4
  • 18
  • 35
  • Are you using CPanel? If so checkout [this image](http://screencast.com/t/MDc5MTJkMGQ), you just have to find the cronjob area on your panel. If you're using a linux server without control panel checkout [this url](http://www.cyberciti.biz/faq/how-do-i-add-jobs-to-cron-under-linux-or-unix-oses/), if you are using windows or need any other help let me know and I'll post a guide. – Diogo Raminhos Apr 27 '12 at 13:05
  • @Whiteagle I am using windows 7 – Faryal Khan Apr 27 '12 at 13:06
  • Is windows 7 your production or development environment? – Diogo Raminhos Apr 27 '12 at 13:07
  • Refer to [this question](http://stackoverflow.com/questions/295386/how-to-run-a-php-file-in-a-scheduled-task-windows-task-scheduler). – Diogo Raminhos Apr 27 '12 at 13:09
  • 2
    Oh, you're on Windows. You can forget about cron and that tutorial then. Try to use Windows Scheduler or something. – Mischa Apr 27 '12 at 13:10
  • @ Whiteagle Didn't get the way that question is answered. Isn't there any alternative for crontab in windows ? – Faryal Khan Apr 27 '12 at 13:18
  • Edited my answer with how to do it on Windows. – devdRew Apr 27 '12 at 13:24
  • -1. It's best not to substantially modify a question in the way you have - that should be another question. Don't forget that a question and answer are for the benefit of the community, and so should be preserved where possible. Always declare what platform you're on, to save people giving answers for the wrong platform. And this question is very easily searchable: "php scheduler windows" will provide hundreds of results. – halfer Apr 27 '12 at 13:57
  • @halfer okay and sorry for the mistake – Faryal Khan Apr 27 '12 at 13:58

3 Answers3

2

IF YOU ARE USING *.NIX:

*nix command:

crontab -e -u USERNAME

If you wanna edit with nano:

env EDITOR=nano crontab -e -u USERNAME where username is the user which will initiate the script.

If you wanna run it hourly:

01 * * * * ...

For Windows 7, as you asked: Start => Accessories => System Tools => Task Scheduler

  • Create task
  • Actions
  • New
  • choose the path to script and don't forget to add php before script, in order to execute, not just open.

To run script with a specified period or by date, use Triggers tab.

And there configure the task to run you're PHP script.

devdRew
  • 4,147
  • 2
  • 21
  • 32
  • Is it possible to run a codeigniter controller by scheduling a task? – Faryal Khan Apr 27 '12 at 13:30
  • Yes, cause in the end running a codeigniter controller is calling script. I've done it for Symfony, say for `cron` controller, I've just configured task to run `cron.php`, and in routing defined my homepage for this controller as pointing on cron action. – devdRew Apr 27 '12 at 13:33
  • There is no option to run the script after every minute or hour. – Faryal Khan Apr 27 '12 at 13:34
  • Yes I found it now I run it every one minute it is running but the quesry is not working... should I edit my question and provide you the code which I want to run ? – Faryal Khan Apr 27 '12 at 13:40
  • What do you mean saying query is not running...? Try to improve your code with logging, and if there were errors, - log it, and debug. – devdRew Apr 27 '12 at 13:44
  • Add `php`, before script path, think you forget it. Check if `php` exists in your system path's. – devdRew Apr 27 '12 at 13:59
  • Where I give the location of the php script while scheduling the task? – Faryal Khan Apr 27 '12 at 14:01
  • Don't know how to show it, but here is my [printscreen](http://devdrew.com.ua/image/scheduler.png) and it works fine, i've tried to execute a symfony's frontend controller. And it's ok. (On the printscreen it says that it never run, but it does, printscreen before F5). – devdRew Apr 27 '12 at 14:05
  • And you defined php in your env variables path? – Faryal Khan Apr 27 '12 at 14:10
  • Computer => Properties => Advanced => Environment variables => System variables => Path – devdRew Apr 27 '12 at 14:19
  • I added it but its still not working my php is is in C:\wamp\bin\php\php5.3.10 and I added this in the enviroment variables under the system variables tab with name = php and vale = C:\wamp\bin\php\php5.3.10 – Faryal Khan Apr 27 '12 at 14:27
  • You don't need to add a variable, just edit `Path` variable, adding `;C:\wamp\bin\php\php5.3.10`, assuming that the php5.3.10 is folder, not binary file. – devdRew Apr 27 '12 at 14:34
1

crontab is a linux program and you say that you are using windows 7; in windows you will have to try a similar thing with scheduled tasks

(that syntax is only for unix)

mishu
  • 5,197
  • 1
  • 21
  • 37
  • Ny idea about how o run it on windows? – Faryal Khan Apr 27 '12 at 13:14
  • 1
    @FaryalKhan you should google about using windows scheduled tasks; most people here are used to running the applications on unix operating systems; if you say that you are using win 7 you should be able to find what you need if you open the start menu and just type "task scheduler".. you will find a program that allows you to set up "cronjobs" using a GUI.. and your script will run using the full path to the php exe and the path to the script as a parameter – mishu Apr 27 '12 at 13:19
0

On your server, you use the command crontab -e, which will open an editor for you where you add the code to your crontab. To have it run every hour, change the line to:

00 * * * * /usr/local/bin/php5 $HOME/system/scripts/clean_cache.php

More details about the crontab format.

cschorn
  • 1,095
  • 10
  • 11