Jump to content

How to bootstrap Processwire in a cronjob?


Recommended Posts

Hello guys,
I have never worked with cronjobs and it seems that after some digging I am almost there, I just need to make sure I include Processwire in the mix. I have included the script and my output under the explanation. Some info on my setup:
– Using Hostinger's hPanel cronjob every hour
– Connected to a script in my templates folder called cronjob.php (The script is tested and it works)
– The script logs some data in cron_test.txt
– It seems everything works until it reaches the first Processwire instatiation and it stops. Unfortunately, no errors are logged in hPanel or Processwire logs and I get no errors if I access a page that is using this template.

The script cronjob.php
 

<?php
namespace ProcessWire;

// =-=-=-=-=-=-=-=-=-=-=-=
// Save the images in pages
// =-=-=-=-=-=-=-=-=-=-=-=

$pw_path = $_SERVER['DOCUMENT_ROOT'] . '/index.php';
include_once($pw_path);

$t = time();
$t = date("H:i:s", $t);
$id = 'test-cron-' . $t;
$thumb_url = 'https://im0-tub-ru.yandex.net/i?id=84dbd50839c3d640ebfc0de20994c30d&n=27&h=480&w=480';
$timestamp = '116161822';
$caption = 'Cron-job-test';

saveImageToDB($id, $thumb_url, $timestamp, $caption);

function saveImageToDB($id, $thumb_url, $timestamp, $caption) {
    $crontext = "Cron Run at ".date("r")." by ".$_SERVER['USER'] ." with PW bootstrapped \n" ;
    $folder = substr($_SERVER['SCRIPT_FILENAME'],0,strrpos($_SERVER['SCRIPT_FILENAME'],"/")+1);
    $filename = $folder."cron_test.txt" ;
    $fp = fopen($filename,"a") or die("Open error!");
    fwrite($fp, $crontext) or die("Write error!");
    fclose($fp);
    echo "Wrote to ".$filename."\n\n" ;
    
    echo "0";
    $p = new Page(); // create new page object
    echo "1";
    $p->template = 'nk-image'; // set template
    echo "2";
    $p->parent = wire('pages')->get('/images/'); // set the parent
    echo "3";
    $p->name = $id;
    echo "4";
    $p->title = $id; 
    $p->save();
    // populate fields
    $p->text_short = $thumb_url; 
    $p->image->add($thumb_url);
    $p->timestamp = $timestamp; 
    $p->textarea = $caption;
    $p->save();

    }

In the hPanel I get the output:

Wrote to /home/u9999999999/domains/domainname.com/public_html/site/templates/cron_test.txt 0 

and in cron_test.txt:

..
Cron Run at Sun, 15 Aug 2021 06:00:02 +0000 by u999999999 with PW bootstrapped 
Cron Run at Sun, 15 Aug 2021 07:00:02 +0000 by u999999999 with PW bootstrapped 
Cron Run at Sun, 15 Aug 2021 08:00:02 +0000 by u999999999 with PW bootstrapped 
Cron Run at Sun, 15 Aug 2021 09:00:01 +0000 by u999999999 with PW bootstrapped 

What am I missing? I feel I am so close 😄

Thank you!

 

Link to comment
Share on other sites

Ok, like @rick said, please try to use the absolute path if possible. If that doesn't work I'd then want to check that the u9...9 user actually has access to PW's files.  If that user is not the same user as the HTTP server user (like apache) then sometimes this can be an issue.

Link to comment
Share on other sites

It worked! I don't know how I missed that. I was pretty sure I checked the path that was generated with docroot, but I guess I won't forget it next time. I am leaving it for a few hours now to test with my actual script and hopefully it's all good.

Thanks for the quick tips, guys!

  • Like 2
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...