1

I want to run a URL every day on 1PM which checks if some variables have been changed by running a query on an external API, and if have changed - store the new vars and notifies me via email.

Workspace:

  • I am using CodeIgniter MVC Framework.

  • Ubuntu Server 15.10.

More information of the situation:

  • The controller function cron_job() is set to public, although I'm not sure it's safe and relevant for being public, since it's a special function that only the server has to run.

public function cron_job(){

// checks for vars and sends updated using email.

}

  • The function is written in a controller that allows you to to run it only if you have a logged_in session. The logged_in session is created using the enctypted function that CodeIgniter offers. Which is kind of cool and easy to use. and safe. So if I want to run this I'll have to add a session somehow before running the cron_job() function. (Create a new private function that adds a session and call the cron_job() function? I'm really not sure whats the right way doing this)

something like this?

private function add_session() {

$date = array(

'email' => $email, // ??? 'is_logged_in' => TRUE

);

$this->cron_job();

}

am i even allowed to run a private function from the server?

or can I insert a session variable using the encrypted functionality of CI and do this easily along with the cron job functionality of my ubuntu server?

Imnotapotato
  • 5,308
  • 13
  • 80
  • 147

1 Answers1

2

Don't use web-based concepts for CLI functionalities ... And in general, don't mess with authentication and logged-in states.

What you should do is move that function into a separate controller, that doesn't require you to be "logged in" and put in a check to make sure that it can only be run via CLI, like this:

class Cli_only extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        is_cli() OR show_404();
    }

}

Also, making a controller method non-public in CI will make it inaccessible from the outside world, so that idea couldn't possibly work.

Narf
  • 14,600
  • 3
  • 37
  • 66
  • Ok, I created a new controller, I want to test if it's working through the server first before setting this in 'cron job'. Do you have any idea how I can run the url from the server to see if it's working? I tried runing "curl" and I get a 404. is there any other way to test this before setting thing on cron_job? – Imnotapotato Dec 06 '15 at 12:38
  • I tried running this using wget too, still isn't working. Running stuff using CronJob uses "Curl" and "Wget" so... I need to get this working. When removing `is_cli() OR show_404();` from the `__construct()` the page runs fine, but still, this controller has to run only by the server! Please help me :( – Imnotapotato Dec 06 '15 at 13:14
  • I made a cronjob for now just to test and i got no changes on my DB + this was the output in the log file: `Dec 6 15:30:01 MYserver CRON[1134]: (root) CMD (curl http://10.0.1.66/tools/Cli_only/cron_job) Dec 6 15:30:02 MYserver CRON[1133]: (CRON) info (No MTA installed, discarding output)` – Imnotapotato Dec 06 '15 at 13:38
  • When you talk about a cron job, I assumed you'd be running it from the same server ... If you need cURL to actually access the application, there's no way to make it accessible to your cron script, but hidden to everything else. – Narf Dec 06 '15 at 22:20
  • I tried running this without curl, I got this output: – Imnotapotato Dec 07 '15 at 08:07
  • `Dec 7 09:47:01 MYserver cron[2663]: (root) RELOAD (crontabs/root) Dec 7 09:47:01 MYserver CRON[6049]: (root) CMD (http://10.0.1.666/tools/Cli_onl$ Dec 7 09:47:01 MYserver postfix/pickup[5837]: C6668102D9A: uid=0 from= Dec 7 09:47:01 MYserver postfix/cleanup[6053]: C6668102D9A: message-id=<201512$ Dec 7 09:47:01 MYserver postfix/qmgr[2480]: C6668102D9A: from=,$ Dec 7 09:47:01 MYserver postfix/local[6055]: C6668102D9A: to=, $ Dec 7 09:47:01 MYserver postfix/qmgr[2480]: C6668102D9A: removed` and no changes on DB – Imnotapotato Dec 07 '15 at 08:08
  • I thought I wasn't doing it right. what do you think the problem is? – Imnotapotato Dec 07 '15 at 08:08