John Davidson

cron - Executing a PHP script in CentOS using Crontab


I am trying to run a Cron job which executes a PHP script in CentOS 8 by using Crontab, but for some reason it is now working.

This is my cron task:

* * * * * root /opt/remi/php74/root/usr/bin/php /var/www/

This is the PHP script:

// Update cron
$test = new PDO("sqlsrv:Server=SERVERIP;Database=DB", "usr", "pass");

$update = $test->prepare("UPDATE table SET Change = 0, Counter = 0");


I also added > result.log at the end of the Cron task to see if it logged something, but this file is empty. Not sure what's happening.

Right now is set to run every minute, but no changes are being made in the SQL Server database

Maybe I am doing something wrong?

My environment is the following:

  • CentOS 8

  • PHP 7.4 (Remi Package)

  • httpd

  • Microsoft SQL Server 2019 Express

My Answer:
// update.php

// Your PHP script code here

echo "Cron job executed successfully!";

Make sure that the PHP script has the correct permissions to be executed by the Cron job. You can set the permissions using the following command:

chmod +x /var/www/

Also, make sure that the path to the PHP binary is correct. You can find the path to the PHP binary by running the following command:

which php

Update the Cron task to use the correct path to the PHP binary:

* * * * * root /usr/bin/php /var/www/

After making these changes, restart the Cron service to apply the changes:

systemctl restart crond

Now, the Cron job should execute the PHP script successfully. You can check the output of the Cron job in the system logs to verify that it is running as expected.

Rate this post

3 of 5 based on 1330 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us