Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
236 views
in Technique[技术] by (71.8m points)

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.

question from:https://stackoverflow.com/questions/65891092/sending-http-request-through-cron-job

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...