Page 1 of 1

[RESOLVED] Vesta - CentOS 7.5 - PHP script is not working in CRON Job

Posted: Thu Jan 17, 2019 5:38 pm
by ruturaaj
Hello,

I am new to Vesta CP. Recently I installed CentOS on UpCloud and installed Vesta CP. I have only one user "admin" and one domain name added. I have set a CRON to run a simple test PHP which writes date and time stamp to a file. The script is located under "domain.com/test/crontest.php".

My CRON command is:

Code: Select all

/usr/bin/php /home/admin/web/DOMAIN.COM/test/crontest.php >> /var/log/crontest.log 
The CRON is set to run this script every 5 min. Here is the PHP code of this script:

Code: Select all

#!/usr/bin/php -q
<?php

$logfile = "cronlog.txt";
$datestamp = date("d F Y, H:i:s") . "\n";
file_put_contents($logfile, $datestamp, FILE_APPEND | LOCK_EX);

?>
Neither the script is working nor any Log is written to /var/log/crontest.log file.

Can someone please guide me with the proper command to run this CRON job? Is this something to do with script execution user rights? How can I fix this issue?

Waiting for your valuable reply...

Cheers,

\r

Re: Vesta - CentOS 7.5 - PHP script is not working in CRON Job

Posted: Fri Jan 18, 2019 4:20 pm
by ruturaaj
Ok, the problem is resolved now. I had to make following changes to solve this issue; sharing it here as this may help someone.

Changes in CRON Command
I made some changes in the command; the revised command looks like this:

Code: Select all

cd /home/admin/web/DOMAIN.COM/public_html/test/; /usr/bin/php -f crontest.php
File Permissions
I checked the CHMOD for the crontest.php and changed it to 755.

User Permissions
I tried the CRON command from SHELL and found some permissions related issues. I added "admin" to "wheel" on my CentOS 7.5 installation and executed CRON command, it worked well this time. Not sure if this was necessary, but I found it on some other forum and so implemented it. Certainly, script started to work after that. Tomorrow, I'm going to try removing "admin" from "wheel" using "gpasswd -d admin wheel" command; let's see if the CRON still continues to work good.

So far so good; the script is running good. Now I'm going to add the actual business logic to this PHP script (some database stuff).

Re: [RESOLVED] Vesta - CentOS 7.5 - PHP script is not working in CRON Job

Posted: Fri Jan 18, 2019 4:44 pm
by grayfolk
PHP best practice use http://php.net/manual/en/function.chdir.php for CLI scripts.

Code: Select all

chdir ( __DIR__ );