John Davidson

cron - Executing a PHP script in CentOS using Crontab

0 comments
Message:


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/mysite.net/html/application/crons/update.php

This is the PHP script:


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

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

?>

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:
php
// 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:

bash
chmod +x /var/www/mysite.net/html/application/crons/update.php


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:

bash
which php


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

bash
* * * * * root /usr/bin/php /var/www/mysite.net/html/application/crons/update.php


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

bash
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

Comments




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