Jump to content

Run ProcessWire URL via command line


DrQuincy
 Share

Recommended Posts

My hosting environment does not allow for PHP to run for more than 120 seconds when it is a web request. However, there is no limit via command line PHP.

Is it possible take a ProcessWire URL such as http://domain.com/foo/bar/?bar=foo and run it as a PHP command line? Something like:

php /path/to/pw/site/index.php route=/foo/bar/ bar=foo

Is such a thing possible? Thanks.

Link to comment
Share on other sites

Thanks, I've seen that. I just wondered if there was a way to — rather then get a page through the API which I could do per your link — get PW to actually process it as though it were a front-end request. If I do wire('page')->get(123) it will retrieve all the data but what I want is to trigger something that is the same as accessing the page through HTTP.

Is there a built-in function for this?

Link to comment
Share on other sites

@DrQuincy what is this page doing that takes a lot of time?

If there is a loop operation that iterates over a lot of items, and you are able to implement an optional get param with the last processed item identifier, there is a good chance that this is all you need to do to make this work.

For this to work you would check if a start-identifier was given with the URL, if not start with the first item, otherwise with the #n item from the $input->get->myidentifiername.
When starting the script, store a timestamp and in each loop operation compare it with the current timestamp. Maybe when it reached the point greater or equal 100 seconds, do a redirect call to the same page, but with the current (last) processed loop item identifier: 

    ...
    if(time() >= $starttimestamp + 100) {
        $session->redirect($page->url . '?itemidentifiername=' . $lastProcessedItemID);
    }
    ...


Or, much easier, have you tested if the 120 second time limit can be reset from within a script? (calling set_time_limit(120) from within a loop can make it run endless on some server configurations)

 

  • Like 4
Link to comment
Share on other sites

Thanks for your suggestion!

In this case it's for something that runs on cron in the night and syncs a website property listing with third-party listing software. It takes a long time to run as it needs to copy images over HTTP and then resize some of them so when the website is accessed during the day it's a lot faster.

Thanks for your suggestion though. I must admit I have never thought of doing it that way — it's a neat solution. If you run curl and then redirect via a session as you have done, does curl honour the redirect? I can't say I've never tried that but from when I have used curl in the command line I'm sure it just returns redirect headers, location, etc.

I just thought maybe there was a way with the API and get a page's rendered HTML as opposed to as an object to force ProcessWire to run the task as it would if you visited it via a browser.

I don't know, I thought maybe there'd be something like $pages->get('/path/to/page/')->render() that returns the page HTML. If so, from my command line script I could simply map the arguments to $_GET and also pass a path.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...