5

Moved the site to another server. Add file statistic.php to the Сron to perform. Only this Cron that does not like something. Write errors:

/home/site/www/statistic.php: line 1: ?php: No such file or directory
/home/site/www/statistic.php: line 2: syntax error near unexpected token `"bd.php"'
/home/site/www/statistic.php: line 2: `include ("bd.php");

There is my code

<?php
include ("bd.php");

    $result = mysql_query("SELECT MAX(id) FROM statistic_dep",$db);
    $myrow1 = mysql_fetch_array($result);
    $last_id=$myrow1[0];
...
ficuscr
  • 6,975
  • 2
  • 32
  • 52
T_E_M_A
  • 560
  • 1
  • 11
  • 28
  • 1
    See this answer: http://stackoverflow.com/questions/1969374/relative-path-not-working-in-cron-php-script – ficuscr Feb 10 '15 at 22:03
  • cron isn't necessarily executing the script from the same directory as the web SAPI, so relative links won't necessarily work.... consider changing directory before your includes, or setting an include path – Mark Baker Feb 10 '15 at 22:03
  • I tried: include('/home/site/www/bd.php'); include('bd.php'); include('/bd.php'); include('./bd.php'); include('../bd.php'); – T_E_M_A Feb 10 '15 at 22:19

1 Answers1

4

Make sure, that you execute the script as php script and not as bash script.

Your crontab should look like this:

* * * * * /usr/bin/php -f /path/to/file.php

Another way to execute the script as php is to add a shebang in the first line:

#!/usr/bin/php
<?php ...
Dirk
  • 436
  • 2
  • 15
  • He is getting a PHP error. Just needs to chdir or use explicit path. – ficuscr Feb 10 '15 at 22:07
  • 1
    Where did he wrote that it's a php error? If you copy his code in a file, make it executable and let bash run it, you get: ./test.php: line 1: ?php: No such file or directory ./test.php: line 2: syntax error near unexpected token `"bd.php"' – Dirk Feb 10 '15 at 22:13
  • My bad. You are absolutely right. -> `?php : No such file or directory` I misread it. Edit that answer so I can change my down vote! Maybe mention shebangs? – ficuscr Feb 10 '15 at 22:17
  • When i written /usr/bin/php -f /home/site/www/statistic.php received this error: No input file specified. – T_E_M_A Feb 10 '15 at 22:32
  • When i written #!/usr/bin/php – T_E_M_A Feb 10 '15 at 22:34
  • @ficuscr I don't understand you – T_E_M_A Feb 10 '15 at 22:40
  • 1
    @T_E_M_A maybe read this: http://serverfault.com/questions/58346/how-to-determine-which-php-is-being-used – ficuscr Feb 10 '15 at 22:41
  • /usr/bin/php -f /home/site/public_html/statistic.php - this work. Thank you so much. Server Support could not help me, and you saved me. – T_E_M_A Feb 10 '15 at 22:48