Welcome Guest, Not a member yet? Register   Sign In
Cronjob via HTTP
#1

Hey there,
I want to run a Cronjob every day, to update one of my mysql tables. I read a lot about it, but there's a problem:

I can run Cronjobs only through a HTTP request or a script.
(site.com/Cronjob/update or /path/to/file.php)

I set up a controller, called Cronjob, to handle my stuff. But I don't want to give access to this controller, so that everyone can execute it.
How would I do this?

I do not have access to cli, so I can not simply check if it is a cli_request..^^

Thanks for helping  Tongue
-.-.-.-.-.-.-.-.- Angel -.-.-.-.-.-.-.-.-
Reply
#2

(This post was last modified: 05-06-2015, 11:25 AM by ivantcholakov. Edit Reason: A typo )

You have to keep in mind two issues:

1. How long the script runs? One minute or two?
2. Have you got an administration panel? Then if it is not CLI, the script can initialize the session and to check whether an administrator has been logged in. On some page within the administrator panel you can place a link to this task.
Reply
#3

(This post was last modified: 05-06-2015, 12:31 PM by Urastor.)

(05-06-2015, 11:24 AM)ivantcholakov Wrote: You have to keep in mind two issues:

1. How long the script runs? One minute or two?
2. Have you got an administration panel? Then if it is not CLI, the script can initialize the session and to check whether an administrator has been logged in. On some page within the administrator panel you can place a link to this task.

1. The script itself only checks, if a date reaches the current day and resets it if needed.. so probably a few ms. I first thought I could do this on every page load, but I think this is kind of silly, because when it has run onces, it won't change anything until the next day.

2. Yep, I do, but I can only include a url or a path to a script, which will be executed. I poorly can change or add anything.^^ The only thing I can add is the interval and the path. I also can add a HTTP user and a HTTP password. But doesn't this mean I need to include a htaccess just for this file?
-.-.-.-.-.-.-.-.- Angel -.-.-.-.-.-.-.-.-
Reply
#4

There's a few things you can try.

Check the request to see if its coming from a specific IP (yours) and deny if not. You'd have to update that whenever your IP changes.
Set some long secret key and pass it via GET.
http://yoursite.com/controller/method?ke...0s09D3jj34

if $_GET['key'] isn't present, or the key doesn't match, throw a 404 error and nothing else.
Reply
#5

A cron job can either, as you mentioned, execute a PHP script over HTTP using a URL or by the command line using a file path. If you use the latter, command line file path, the is_cli_request() will be TRUE. Any script executed by the command line is a CLI request.

CLI here does not mean it you specifically have access to the command line (SSH). It is used to let your script know if it was executed by a command line command, like cron.

It sounds like you use some type of hosting control panel like cPanel, Plesk or such. You should be just fine to use a local path to the script and then use is_cli_request() in your script to block any execution over HTTP.

Also, as a note. When executing over command line, you should set your path to
Code:
path/to/ci/index.php <controller> <function>
where <controller> = the name of your script controller, and <function> = The function in that controller.
Reply
#6

There are also services available to automate cronjobs via HTTP, which use dedicated IPs to send the requests from:
https://www.setcronjob.com/
Reply
#7

@Silentium:
Yes, this is what I know already, but I tested it once with http and with the file method. The file method gave me an error right away, because /path/to/index.php cronjobs update is not a file.

For the http request I created this method in the cronjobs Controller:

PHP Code:
   public function update_news_until_data()
 
   {
 
       if($this->input->is_cli_request() === TRUE)
 
           echo 'cronjob ausgeführt';
 
       else
            echo 
'kein cli request';
 
   


when executing it through the cronjob (yeah, I can directly execute it via the interface) i get 'kein cli request' = no cli request.

Or did I get you wrong?

@CroNiX:
Hm.. the key could be a possibility, but I think I will wait for some more feedback, but I keep this in mind. Wink

For the other answer.. I don't think it will do some difference, because my webhost can execute cronjobs via http and make them in a interval. Tongue The problem is, that everyone else who knows the controller/method, can execute it, and this is something i don't want.
-.-.-.-.-.-.-.-.- Angel -.-.-.-.-.-.-.-.-
Reply
#8

You can also set a custom HTTP header that gets sent with the request from your server and check for it in the controller.

The benefit of using an external service to execute your cronjobs is you don't have to worry about uptime/server problems in case something is wrong with your local machine, or net connection, etc. Some of them are free to use for up to x requests/day, like the one I linked to. Others are more secure as they use an authentication system, which your app can check for.
Reply
#9

(05-06-2015, 03:31 PM)CroNiX Wrote: The benefit of using an external service to execute your cronjobs is you don't have to worry about uptime/server problems in case something is wrong with your local machine, or net connection, etc. Some of them are free to use for up to x requests/day, like the one I linked to. Others are more secure as they use an authentication system, which your app can check for.

No, no, I use a external host, so I don't think there are issues. I also think, if the server has problems with the connection, the cronjob should not be executed.
But I get the point what you're trying to say.

On the other hand a auth system to validate the request would be kinda cool..


(05-06-2015, 03:31 PM)CroNiX Wrote: You can also set a custom HTTP header that gets sent with the request from your server and check for it in the controller.

You mean executing a script in a folder, which sends a http request?

I first thought of executing a script in a secure folder, which uses curl to execute the url.
-.-.-.-.-.-.-.-.- Angel -.-.-.-.-.-.-.-.-
Reply
Reply




Theme © iAndrew 2016 - Forum software by © MyBB