0

I'm trying to run a cronjob (my first time ever) but it isn't working as expected. The PHP file I'm trying to run, contains a tiny code which sends an SMS message every time it is executed. When I open the URL to the file on my browser, it works perfectly and sends the SMS message. But the cronjob doesn't.

Here's the command I use:

/usr/local/bin/ea-php56 /home/MY_USERNAME/PATH_TO_A_SUBDOMAIN/crontest.php

I wrote the command using the guide cPanel provides at the exact same page you define a cronjob. So I basically think there's no syntax error there. Needless to say I checked the timezone for the server and compared to one I set for the cronjob.

EDIT

Here's the code existing on the PHP file:

session_start();
require_once 'maincore.php'; // contains some funtions and settings
include_once INCLUDES.'SendMessage.php'; // contains required functions for sending the SMS.
//
and some other codes here to eventually send the SMS, which works flawless
//

UPDATE

Ok so, since I added that CronJob, the CPU Usage and Disk Space Usage on my hosting panel boosted to the skies. So I tried checking my error log to identify the cause.

Now let me add some more information. The maincore.php file which I mentioned, contains some functions and also the following code:

$folder_level = "";
while (!file_exists($folder_level."config.php")) { $folder_level .= "../"; }
require_once $folder_level."config.php";
define("BASEDIR", $folder_level);

There are 2 things to point out here:

  1. config.php file contains database information like username, password, etc.
  2. That code tries to find the config.php file and include it. In case it doesn't find the file, it will go back 1 folder each time and tries finding the file again in case the maincore.php was misplaced.

As it turned out, the code I just mentioned, causes an endless loop which refers to file_exists function.

The error log contains lots of the following errors:

PHP Warning:  file_exists(): open_basedir restriction in effect. File(../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../.. in /home/MY_USERNAME/PATH_TO_A_SUBDOMAIN/maincore.php

PHP Warning:  file_exists(): File name is longer than the maximum allowed path length on this platform (4096): ../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../../ in /home/MY_USERNAME/PATH_TO_A_SUBDOMAIN/maincore.php
Kyan
  • 65
  • 1
  • 8
  • Just to clarify: you replace `MY_USERNAME/PATH_TO_A_SUBDOMAIN` with actual values? – Justinas Sep 02 '20 at 06:32
  • @Justinas Yes of course :) – Kyan Sep 02 '20 at 07:02
  • Is your code independent from web context? Like using `_GET`, sessions, index file? – Justinas Sep 02 '20 at 07:12
  • @Justinas I edited the question and added the codes on the PHP file as well. – Kyan Sep 02 '20 at 07:29
  • Try adding `2>/home/MY_USERNAME/PATH_TO_A_SUBDOMAIN/errors.log` to see if any errors happends – Justinas Sep 02 '20 at 07:31
  • @Justinas If you wouldn't mind, please read the update on the original post. – Kyan Sep 02 '20 at 16:15
  • So your `while (!file_exists($folder_level."config.php"))` never finds file. Ensure it exists. Also put initial `$folder_level` to be `__DIR__ . '/'` to ensure that you start at current file location. Better get rid of that "find where config is" and use absolute paths via `__DIR__`, `dirname(__DIR__)` etc. – Justinas Sep 02 '20 at 19:46
  • @Justinas It's getting stranger each time I try something. So, this time, I removed all codes from the file and merely put these 2 lines of code: `fopen('mytestfile1.html', 'w');` and `fopen(__DIR__ . '/'.'mytestfile2.html', 'w');` I used 2 versions of the code to ensure the current directory will be used. But none of the files were created! Needless to say that I used the exact same Cron Job command, and that files are created when I write the exact url on my browser and open the file manually. – Kyan Sep 03 '20 at 08:34
  • Put `__DIR__` everywhere where you refer to file. Also ensure that your PHP script, that possible is running under different user than when accessing through web, has all permissions to create/read files: https://stackoverflow.com/questions/7261566/recursively-chmod-chown-chgrp-all-files-and-folder-within-a-directory – Justinas Sep 03 '20 at 08:40

0 Answers0