Schedule scripts without using CRON

I know that there are many reports about using CRON to run a php file. But, in the world of shared hosting and ease of configuration for the user, I do not want this to be connected.

I found another solution on the Internet that is related to sockets. I just wanted everyone to take it, and tell me if this is a good or bad idea. It seems to work well.

Thoughts?

//Open socket connection to cron.php $socketcon = fsockopen($_SERVER['HTTP_HOST'],80,$errorno,$errorstr,10); if($socketcon) { $socketdata = "GET /cron.php HTTP 1.1\r\nHost: ".$_SERVER['HTTP_HOST']."\r\nConnection: Close\r\n\r\n"; fwrite($socketcon,$socketdata); //Normally you would get all the data back with fgets and wait until $socketcon reaches feof. //In this case, we just do this: fclose($socketcon); } else { //something went wrong. Put your error handler here. } 

cron.php:

 //This script does all the work. sleep(200); //To prove that this works we will create an empty file here, after the sleep is done. //Make sure that the webserver can write in the directory you're testing this file in. $handle = fopen('test.txt','w'); fclose($handle); 

Found the script from the blog post: http://syn.ac/tech/13/creating-php-cronjobs-without-cron-and-php-cli/

+14
scripting command-line-interface php cron
Jan 27
source share
7 answers

This is not a bad method, but you need to make sure that by closing the socket, it does not just terminate the script before it is complete. You can set sockets to non-blocking ones.

I will still use the cron job, even if it's a little pain.

+3
Jan 27
source share

The cron task is the cron task. You have configured it, and the OS will complete the task for you. I'm not sure how the PHP script from the site works, but if it requires human intervention, then it is not really called cron work. If you do not want to use cron, you can use a loop and then use the PHP date functions to set the date and time. Pseudo code

 while (1) { $d=date("d"); if ( $d == "01" ){ //run every 1st of month //code to run here } } 
+4
Jan 27 2018-10-01T00:
source share

This produces a different effect than cron.

The cron task starts at certain points in time that you set in advance.

Your method is basically a kind of “fork” or “asynchronous call” for a PHP script. Doing this through HTTP, as you do here, is a cheap and easy technique. I use it myself. It differs from cron in that it immediately launches a "background process".

A few comments:

  • First of all, you should call ignore_user_abort() in the "background" script. Otherwise, in many environments, your script will be interrupted when the "calling" script closes the socket.

  • Secondly, you can actually check the variable $_SERVER['HOST'] in the "background" script, and thus you can have scripts that are not exposed to the Internet (basically issue localhost requests and check this in the background script). Then you can probably trust the requests coming from your own machine and skip all security checks, sessions, etc.

  • Thirdly, who says that the "background" script should run with PHP? PHP has many drawbacks if you intend to use it as a "background" process. The main disadvantage is that it blocks I / O. Therefore, if you are going to send emails, update database rows or something else, you basically pause your script every time you send a request. If, for example, Node.js, you can run I / O commands asynchronously and continue to work. If you intend to use PHP, at least be sure to send 10 emails at a time or update 10 lines at a time or something like that.

  • Finally, you can display the progress bar in the browser if the background script is doing something. Thus, you will need to use a common data store (for example, your database) to record the progress for the task.

+2
Nov 20 '10 at
source share

This is a functional, but strange solution that ensures that your machine is connected all day, starting the first script. If you want this, I would recommend that you use a shell script using wget or curl for this purpose.

For example:

 #!/bin/sh curl -O http://www.myserver.com/cron.php 2>&1 > /var/log/remote.cron.log 

But I think that the solution you would like to implement if you do not need to run very synchronously is to check at the end of your index.php to see when it ran the script for the last time, if it was more than two hours back, then include('cron.php') . You can also save the timestamp when the script ran in an environment variable to avoid a performance penalty.

+1
Jan 27 2018-10-01T00:
source share

As I understand it, after reading this blog post and looking at the code, this does not really mean that you do not have access to cron, it is a way to avoid someone waiting for the server to respond to a long request. If you absolutely must have a specific script executable say every 10 minutes or so, then you will need to use cron. If you just want users to not wait for the long request to complete, this hack might work. Even with this method, I still think that you will hit time limits if your script takes longer than PHP allows.

Revision of wp-cron.php (mentioned in the blog link), it seems that it relies entirely on users visiting the site to run checks on a given type of job.

These methods will not be very reliable unless you have another ping server at regular intervals. The main purpose of the method is to avoid waiting for the user, for example, like 15 seconds for cleaning or maintenance scripts that run every time for a while, and not as a true replacement for all kinds of cron uses.

+1
Jan 27
source share

If I understand well, you would run the first script from a remote machine, striking the second script, which will be hosted on your cron-disabled node. Then, due to an error or a strange php / webserver interaction function when the connection is closed immediately, the script will not time out?

The first part is a fairly common practice, there are even companies providing this service ( http://www.webcron.org/index.php?lang=english , for example, automatically pops out any script you want at any time when you ask for it, for a fee).

The second part is unknown to me. This seems like a bug in the php / webserver interaction, but I could be wrong. In any case, I would double-check whether this is a mistake or not (wait, what are you doing right now?), And if that turns out to be legal behavior, then go for it. If this seems like a mistake, then do not rely on it, as it can be fixed at any time.

0
Jan 27 '10 at 0:45
source share

It took me several days to find a working solution without the conditions of a race and / or flood of my own server, but in the end, I think this should work:
http://www.programmierer-forum.de/phpcron-cronjobs-ohne-crontab-t348377.htm

0
Feb 12 '15 at 9:33
source share



All Articles