0

I'm making a reminder system which can send emails at a date and time set in each reminder.

I want there to be an automated function that executes this email-sending script in the background, so that while the server is on, this function will run without interrupting other processes.

Edit: I am running PHP on Windows 10.

caitlin
  • 2,769
  • 4
  • 29
  • 65
adoh
  • 11
  • 5
  • Maybe you can use cron jobs to execute a php script instead of having a loop on a php process. Take a look at http://www.thegeekstuff.com/2011/07/php-cron-job/ – Rubén Cougil Mar 21 '16 at 15:20
  • I see some tutorials about this cron. But mostly they use Linux, while I am using windows. Anyway, I will try it. – adoh Mar 21 '16 at 15:42
  • On windows, the task scheduler should provide an equivalent functionality. It would be appropriate to include information about your platform as well when asking questions like this one. – syck Mar 21 '16 at 15:50
  • 1
    Possible duplicate of [how to create php scheduled process?](http://stackoverflow.com/questions/1175494/how-to-create-php-scheduled-process) – Alessandro Da Rugna Mar 21 '16 at 16:10
  • Thanks syck, I edit my question already. Alessandro, that one doesn't belong to me. But it gives me more information. Thank you :) – adoh Mar 21 '16 at 16:32

2 Answers2

0

You need to set a cron job to run your code every X minute/hour/day/...

Create a method in a controller and put your code inside it. Then set your cron job to run it at a special moments.

If you have cPanel or other management panels it would be easy.

Edit #1: To run in CLI you should run index.php using php command and send controller and action name as parameters. If you have a controller named User and a method named "happy_birthday", then you need to run this command: php index.php users happy_birthday.

Ali Farhoudi
  • 5,350
  • 7
  • 26
  • 44
  • how would you access this controller method through CLI ? – Vickel Mar 21 '16 at 15:43
  • I try to use CLI with command as you write. But it doesn't work. The error message is : 'php' is not recognized as n internal or external command. – adoh Mar 23 '16 at 04:26
  • In windows you have 2 options: #1: add php to your environment path (when you add a new directory to your environment path, windows tries to search that directory for all commands, then when you add php windows will search new directory added to find php command. To do this open "System Properties" or "Advanced system settings" (in win 7 right click on My Computer and press "Properties"), open tab "Advanced", then press "Environment variables" and in the opened window in "System variables" section find "Path", select it and press "Edit" and add php path (e.g "C:\wamp\bin\php\php5.5.12\")). – Ali Farhoudi Mar 23 '16 at 07:06
  • And option #2: you can use php command with absolute path: ```C:\wamp\bin\php\php5.5.12\php index.php users happy_birthday``` – Ali Farhoudi Mar 23 '16 at 07:06
  • I have tried both options. The script is running But without any result :( The script just executed, without any error message nor result – adoh Mar 23 '16 at 14:41
  • Try these: 1. Run your action using browser to check if it is working and has result. 2. Debug your code by putting some echo in your action and they should be printed in console. 3. Add your code to your question so that we can take a look at it. – Ali Farhoudi Mar 23 '16 at 15:43
  • It works already!! Thanks All! In the beginning, I try to run via browser and it works, but if I run via CLI there is no result. even by echo, not display anything Then I try to change the script become : >php "c\xampp\htdocs\project\index.php" controller_name method And, it works!! :D – adoh Mar 24 '16 at 05:03
0

Carrying on from Ali Farhoudi answer.

You need to use authorisation of $this->input->is_cli_request()

https://ellislab.com/codeigniter/user-guide/general/cli.html

The is_cli_request() will make sure that you are running the command from the server, IE no one has just navigated to that URL and ran the script.

Most hosting services have an easy method to do this (run a cron). You may find a bit of trial and error at first, as your development server will probably differ to production. So what works on your machine to run a script at terminal / whatever, won't be the same as the server for obvious reasons.

So its just a controller like any other, that you server, at a given time, runs.

Mine look something like this, set inside a cron

php-cli /home/a_subfolder/public_html/another_subfolder/myscript.php

This is running on a Linux server, so the codeigniter question part is above, how you run a cron in windows, don't know / care

check this.

What is the Windows version of cron?

I'm guessing your running this locally only then? Judging by windows 10.

Community
  • 1
  • 1
Rossidge
  • 5
  • 1
  • 5
  • Ah, You're right. now I am using windows 10 in my local machine. But I will upload to a server with Win XP – adoh Mar 21 '16 at 21:04
  • I really don't know anything about a server on XP, or Windows for that matter, but are you sure its on XP? Sounds old to me. Anyway get it right on your local machine first, use something like helper function log_message('error', 'you managed to call me via XP miracle') then you know your hit that controller when you do. – Rossidge Mar 21 '16 at 21:18
  • Yes, I am trying to use Windows Task scheduler to run CLI. But it still doesn't work – adoh Mar 23 '16 at 04:29