[英]telegram bot with cron and PHP script
在 EC2 上
我有一个这样的 crontab
* * * * * /usr/bin/php /opt/bitnami/apache2/htdocs/bot.php
bot.php 文件
1 <?php
2
3 exec( 'touch /opt/bitnami/apache2/htdocs/testCron.txt');
4
5 $botToken="xxxxxxxxxxxxx";
6
7 $website="https://api.telegram.org/bot".$botToken;
8 $chatId=123456;
9 $params=[
10 'chat_id'=>$chatId,
11 'text'=>'test bitnami',
12 ];
13 $ch = curl_init($website . '/sendMessage');
14 curl_setopt($ch, CURLOPT_HEADER, false);
15 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
16 curl_setopt($ch, CURLOPT_POST, 1);
17 curl_setopt($ch, CURLOPT_POSTFIELDS, ($params));
18 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
19 $result = curl_exec($ch);
20
21 if ($result === FALSE) {
22 echo 'An error has occured: ' . curl_error($ch) . PHP_EOL;
23 }
24 else {
25 echo $result;
26 }
27
28 curl_close($ch);
29
30 ?>
testCron.txt 被创建,以便 cronTab 工作。
当我用 firefox 打开 bot.php 时,API 调用有效并且机器人在 Telegram 上发送了消息,但是带有 curl 的 PHP 脚本不适用于 Cron。
我将 htdocs 和 bot.php 权限设置为 777 进行测试,但它不起作用。
任何的想法?
提示:我将其添加为答案,因为我的声誉(截至目前)低于 50。
截至目前,我需要更多信息才能提供支持。 这里可能存在多个潜在问题。 看看我们能不能一起走下去。
1. 在 cron 执行期间使错误可见
在这种情况下,您可以检查您的 cron 中的日志。 我建议通过 this to your cron >> /var/log/my-cron.log 2>&1
将任何输出记录到文件中。
* * * * * /usr/bin/php /opt/bitnami/apache2/htdocs/bot.php >> /opt/bitnami/apache2/htdocs/testCron.log 2>&1
从这里我们可以检查是否有任何执行错误、权限错误或一些错误配置。
2. 检查您的错误日志配置
可能是您的错误未显示。 这是一些配置的一个很好的起点
// tells your script to report any error
error_reporting(E_ALL);
// tells your script to enable error logging and the location where it should log to
ini_set('log_errors', '1'); // tells your script to enable logging
ini_set('error_log', '/dev/stderr');
让我们看看这是否足以获得初步想法,或者我们是否需要更深入地挖掘。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.