Faced with a strange phenomenon for me.

There is a php-script, which when launched, pulls out some values ​​from the database, forms a variable and sends its value through a bot to Telegram.

If the script is run manually through the browser, the whole message arrives in Telegram, as it was intended.

If the script is launched by cron, then a message without a variable value arrives in Telegram.

The following is written to cron:

59 23 * * * /usr/bin/php /var/www/myhost.ru/public_html/_tze/cron_script.php 

Script code:

 $message = "Повестка дня:\r\n"; $i=1; $_tasks = mysqli_query($connection, "SELECT `title` FROM `tze_tasks` "); while ($tasks = mysqli_fetch_assoc($_tasks) ) { $message = $message."\r\n".$i.". ".$tasks['title']; $i++; } file_get_contents('https://api.telegram.org/bot276781699:AAFY6PZ7icBbHP1XZS582Pt RzHGhoPezb6U/sendMessage?chat_id='.$chat_id.'&text='.urlencode($message)); 

What could be the problem?

  • one
    How do you call the script? Add the line - progmb
  • one
    The variable, probably, is created by the http-server. then run the script not with the php interpreter, but with a request to the http server via a browser: wget , curl , etc. - aleksandr barakin
  • 3
    The problem is in the script. Somewhere. But the script is secret. Because they will not be able to help you with anything - Anton Shchyrov
  • 2
    Script code show. The place where the variable is defined and used is rjhdby
  • one
    @ user229633 Judging by the script, it is written completely without taking into account the possibility of launching php_cli - here you can both recommend such a revision, and leave the crutch running via wget. If programming is not your profession, you can stop at the version that Alexander described. If you are a programmer, you can write a variant of the script that will work in both modes. The more links in the system, the greater the risk that it will break somewhere. Even if it is reliable wget and the Internet is not particularly needed here. (They'll screw up something in the firewall rules - and you will guess what's broken.) - AK

2 answers 2

update due to the content of the script laid out: it is clearly not intended to run in the “standalone” mode (a connection to the database is used somewhere in the depths of the site), and therefore the only reasonable solution is to use a browser and access http- server, i.e. Option number two of the following.


without variable value

From this we can conclude that we are talking about one of the many environment variables set by the http-server before calling the php interpreter.

output two:

  1. You can specify this variable and its value when invoking the interpreter:

     имя=значение /usr/bin/php ... 
  2. or you can contact the interpreter not directly, but via the http-server using some kind of “browser” - wget , curl , etc .:

     wget -qO /dev/null http://ваш.сервер/ваш.скрипт.php >/dev/null 2>&1 
  • The solution is actually the same: write a normal script, taking into account both possible launch modes. And these two options are pure crutches. However, if the extra overhead to launch wget is not a pity and "People hawala" - let it be. - AK

It may be necessary to add at the beginning of the script:

 #!/usr/bin/php 

but then it will not run through the browser

  • I agree with the minus one: your answer is wrong. You already call via php filename.php - and nothing will change from the handler's statement. - AK
  • @AK, "minus", by the way, put the "community spirit" after making changes. - aleksandr barakin
  • @alexanderbarakin Ah, I thought that your minus. - AK