Post by Joe1234 » Tue Feb 11, 2025 12:21 pm

I'm trying to setup a file to be executed by cron...not going so well. The primary function of this file is to do database stuff. In the past the only way I got cron to work is to make it completely independent of opencart....so it just contained raw php coding, no includes, functions, etc. But with what I'm doing now I'd prefer to be OC compliant with this particular file, especially since I want to easily retrieve some database config settings. So how do I bootstrap OC to a file so it can be executed with a cron with parameters, and utilize OC functions?

Note: wget with quotes around the url wont work for me, for some reason once I put quotes around the url the command wont execute and the & that goes with the parameters causes it to fail.

This is what I tried, among many other things, I tried to simply put the main points of the index file at the top of the cron file so I can use the opencart functions in the file, but it fails at "start('catalog');", I even tried putting the absolute path to catalog.

Here's the file:

Code: Select all

<?php

$message = "This is a log message cron test.";
$logFile = "/home/***/***/test.log";

// DIR
// Configuration
if (is_file('/home/***/***/config.php')) {
	require_once('/home/***/***/config.php');
}


// Startup
require_once('/home/***/***/system/startup.php');


error_log("A" . "\n", 3, $logFile);
//start('/home/chris850/site_plks/catalog');
error_log("B" . "\n", 3, $logFile);


// Initialize OpenCart registry and other core objects
$registry = new Registry();
$loader = new Loader($registry);
$config = new Config();
$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);



$registry->set('load', $loader);
$registry->set('config', $config);
$registry->set('db', $db);

//$this->log->write("******************************************************CRON CONDITION 1: ");


// Get the controller
$controller = new ControllerExtensionCommonMyScript($registry);


// Get the 'reset' parameter
$resetValue = null; // Initialize to null
//$this->log->write("******************************************************CRON CHECK 2: ");
error_log("1" . "\n", 3, $logFile);

if (isset($argv[1])) {
    $parts = explode("=", $argv[1]);
    $resetParam = $parts[0];
    if ($resetParam === "reset") {
error_log("2" . "\n", 3, $logFile);

        $resetValue = $parts[1];
    } else {
error_log("3" . "\n", 3, $logFile);

        log_error("Invalid reset parameter: " . $argv[1]);
        exit(1);
    }
} else {
error_log("4" . "\n", 3, $logFile);

    log_error("No reset parameter provided.");
    exit(1);
}


$_GET['reset'] = $resetValue;
//$this->log->write("******************************************************CRON CHECK 2: " . $_GET['reset']);

// Call the function within a try-catch block
try {
error_log("5" . "\n", 3, $logFile);

    $controller->cron_reset();
} catch (Exception $e) {
    log_error("Error in cron_reset(): " . $e->getMessage()); // Log the exception
    exit(1); // Exit with a non-zero code
}


// Function to log errors (you can customize this)
function log_error($message) {
    $timestamp = date('Y-m-d H:i:s');
    error_log("{$timestamp} - {$message}\n", 3, $logFile); // Log to file
    // You could also log to OpenCart's log system if you have it available:
    // $registry->get('log')->write("CRON ERROR: " . $message);
}

?>
Last edited by Joe1234 on Thu Feb 13, 2025 11:15 pm, edited 1 time in total.

v3.0.4.0 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by JNeuhoff » Tue Feb 11, 2025 6:33 pm

Just write it up as a normal OpenCart extension, with a new route like extension/module/mycrontask. And for security purposes, restrict it to only allow execution when the request has come from the server's IP-address, not from the public.

Export/Import Tool * SpamBot Buster * Unused Images Manager * Instant Option Price Calculator * Number Option * Google Tag Manager * Survey Plus * OpenTwig


User avatar
Guru Member

Posts

Joined
Wed Dec 05, 2007 3:38 am


Post by paulfeakins » Tue Feb 11, 2025 8:55 pm

Several times you say "won't work" or "fails" without telling us what exactly it does or outputs. You haven't even shown us the wget command.

Seriously, you've been here long enough to do better than this. Have another go at explaining.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Legendary Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by Cue4cheap » Tue Feb 11, 2025 11:43 pm

Joe1234 wrote:
Tue Feb 11, 2025 12:21 pm
I'm trying to setup a file to be executed by cron...not going so well. The primary function of this file is to do database stuff. In the past the only way I got cron to work is to make it completely independent of opencart....so it just contained raw php coding, no includes, functions, etc. But with what I'm doing now I'd prefer to be OC compliant with this particular file, especially since I want to easily retrieve some database config settings. So how do I bootstrap OC to a file so it can be executed with a cron with parameters, and utilize OC functions?

Note: wget with quotes around the url wont work for me, for some reason once I put quotes around the url the command wont execute and the & that goes with the parameters causes it to fail.

This is what I tried, among many other things, I tried to simply put the main points of the index file at the top of the cron file so I can use the opencart functions in the file, but it fails at "start('catalog');", I even tried putting the absolute path to catalog.
Single or double quotes for the wget?
What is the wget command string?
Mike

cue4cheap not cheap quality


Expert Member

Posts

Joined
Fri Sep 20, 2013 4:45 am

Post by Joe1234 » Wed Feb 12, 2025 8:00 am

@JNeuoff that's how I originally had it, standard controller, it was executing without passing the parameter when called by wget cron command. So I changed the function to this so I can pass the parameter by going with the absolute path php command route, but in doing that I have to bootstrap and initialize OC to use its functionality....from what I understand.

@paul fail as in it doesn't work, it's not executing, that's all I have for you.

Code: Select all

wget https://website.com/index.php?route=extension/common/my_extension --- works
wget 'https://website.com/index.php?route=extension/common/my_extension' --- doesn't execute
wget "https://website.com/index.php?route=extension/common/my_extension" --- doesn't execute
--------------------------------------------------------
wget https://website.com/index.php?route=extension/common/my_extension&param=value --- works, doesn't pass the parameter
wget 'https://website.com/index.php?route=extension/common/my_extension&param=value' --- doesn't execute
wget "https://website.com/index.php?route=extension/common/my_extension&param=value" --- doesn't execute
----------------------------------------------------------
wget -q -O - https://website.com/index.php?route=extension/common/my_extension?param=value > /dev/null 2>&1 --- works, doesn't pass the parameter
wget -q -O - 'https://website.com/index.php?route=extension/common/my_extension?param=value' > /dev/null 2>&1 --- doesn't execute
wget -q -O - "https://website.com/index.php?route=extension/common/my_extension?param=value" > /dev/null 2>&1 --- doesn't execute
I tried a few other "creative" ways, all to no avail. To be clear, doesn't "detect" the parameter. OC says param doesn't exist.

v3.0.4.0 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am

Post by nonnedelectari » Wed Feb 12, 2025 2:48 pm

Instead of recreating the OC framework in order to launch cron job, I would suggest you figure out why

Code: Select all

wget 'https://website.com/index.php?route=extension/common/my_extension&param=value' --- doesn't execute
as there is no reason why it shouldn't.

Active Member

Posts

Joined
Thu Mar 04, 2021 6:34 pm

Post by softmonke » Wed Feb 12, 2025 3:27 pm

Why are you re-initialising all the core OpenCart classes? Perhaps as a starter, try creating a new file: "catalog/controller/tool/cron.php".

And inside this file:

Code: Select all

<?php
class ControllerToolCron extends Controller {
    public function index() {
        echo "Hello World!";
    }
        
    public function example() {
        echo "Hello Example!";
    }
}
Test the URLs in your browser: www.your-domain.com/index.php?route=tool/cron for index function and www.your-domain.com/index.php?route=tool/cron/example for example function.

This is something very trivial - there's no need to overcomplicate things.

Check out our ever-growing list of extensions for OpenCart here.
Some useful extensions for a better admin experience: Image File Manager ProDrag & Drop Sort Order

Reach out to us at hello@softmonke.com for your OpenCart web development needs or feedback for our extensions.


User avatar
Active Member

Posts

Joined
Tue May 23, 2023 4:42 am


Post by paulfeakins » Wed Feb 12, 2025 6:42 pm

Joe1234 wrote:
Wed Feb 12, 2025 8:00 am
@paul fail as in it doesn't work, it's not executing, that's all I have for you.
Nope, there's loads more information you could give about this.

So you could say, I'm running in the terminal on the server and when I hit return there is no output and as far as I can tell the URL isn't being it, if that is indeed what happens.

But as others have said, the command looks good so it should be hitting the URL - you could add some logging at the top to check.

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Legendary Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom

Post by Joe1234 » Wed Feb 12, 2025 11:00 pm

@paul, yeah, I don't really touch the terminal, so I don't really know it's functionalities. All I know is the cron command doesn't execute(url not hit) because I'm getting no printout from the logging when there are quotes, nothing logged in my server error logs, and I'm getting no parameters passed without the quotes because when I do a retrieval of the parameters they don't exist. Going directly through the browser url everything works fine. So at the end of the day as far as I see and know all I see is it doesn't execute. Now if there is more information that you see is needed and know how to get, that would be great information to impart on me so I could retrieve it for you.

I'm more than sure this is a host problem because as others have confirmed, there is nothing wrong with the command with quotes, but I'm far from a cron expert and just can't be bothered at this moment with the back and forth with them to fix this when I have other things to do and there is another solution. I will circle back around to them to get into this fight that it is their issue and not a coding issue which is always their default and a hassle.

Either way, I got it working pretty much the way I need to by including the config file so I could get the db credentials, which was the most important part, by doing php absolute path command for the cron. I didn't do it this way initially because I've gotten used to doing full url with wget on opencart, and I thought I needed more functions from other OC files. Everything else I really needed the bootstrap for was for OC query functions and OC logging which wasn't an absolute need.

This time I caught a break, but next time I may need to include a couple controllers or models, so I'd still like to know why the "start('catalog');" wasn't working. @softmonke, don't I need to initialize what's in the index in order to load a controller like "$controller = new ControllerExtensionCommonMyScript($registry);"?

@JNeuoff in doing it the way I just set it up there doesn't seem to be an IP address registering, so should I then code it to look for no IP address to allow the execution of the script?

Thanks.

v3.0.4.0 php 8.1
I'm here for a reason, if your response is contact a/the developer, just don't reply.


Active Member

Posts

Joined
Sat Jan 01, 2022 5:47 am
Who is online

Users browsing this forum: No registered users and 69 guests