php - Sending HTTP request through cron job

I have a PHP file that is deployed in a server:
/var/www/html/cron/leave_mail.php
The code inside this PHP file is just sending a request via CURL (The server of the url is the same server of the cron job):
<?php
$url = "http://my-site.build.com/sendmail/sendmail_leave";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$head = curl_exec($ch);
curl_close($ch);
?>
Now I created a cron job on the same server where I deployed the PHP file:
* * * * * php /var/www/html/cron/leave_mail.php
After 1 minute, it didn't execute the command inside the PHP file, however, I can see on the system logs that the cron job runs.
I run my local virtual machine with ubuntu os and created the same cron job, executing the same file. After 1 min, I can see that it executes the command inside the PHP file because the data on the database was updated.
I'm really puzzled on why the cron job on the server didn't executes the PHP command. I'm stuck on this issue.
Answer
Solution:
Start with finding where PHP is:
which php
Then update the crontab by running the following command:
crontab -e
Then update the line with the correct PHP path:
* * * * * root /usr/bin/php /var/www/html/cron/leave_mail.php
Also note that you can specify the user before the path. Hopes this helps a little.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: script cache:clear returned with error code 255
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.