How to execute php script via cron?

The rights to the script stand.
Server - ubuntu.
using the console to open crontab crontab-e,
then write to every 5 minutes running a script
*/5 * * * * /usr/bin/php /var/site.ru/script.php >/dev/null 2>&1

Myself a php script to delete files in one folder. And if from the browser to open it, the files are deleted. Crowns and something doesn't want to do it.
Went to var/log/syslog and there are records every 5 minutes, but how to understand what error I do not understand.
June 5th 19 at 21:43
3 answers
June 5th 19 at 21:45
Solution
well, first, try to run the script manually from the console just like trying in the crown, i.e. by entering the command /usr/bin/php /var/site.ru/script.php and look at that answer.
PHP Warning: opendir(/form/uploads): failed to open dir: No such file or directory in /var/site.ru/script.php on line 5
here he php script
<?php
$dir=$_SERVER['DOCUMENT_ROOT']."/form/uploads"; //read this directory 
$todel=60; // time to delete 
if($OpenDir=opendir($dir)){
 while(($file=readdir($OpenDir)) !== false){ 
 if ($file != "." && $file != ".."){ 
 $dtime=intval(time()-filectime("{$dir}/{$file}")); 
 if ($dtime-->=$todel) {unlink("{$dir}/{$file}");} 
 } 
 } 
 closedir($OpenDir); 
}
- jaylon_Hahn74 commented on June 5th 19 at 21:48
it turns out that instead of $_SERVER['DOCUMENT_ROOT'] something else to use? - jaylon_Hahn74 commented on June 5th 19 at 21:51
Yes - kenny commented on June 5th 19 at 21:54
what?) - jaylon_Hahn74 commented on June 5th 19 at 21:57
and what do you have by the way there is generally obtained in $dir? - kenny commented on June 5th 19 at 22:00
,
/var/site.EN/form/uploads - jaylon_Hahn74 commented on June 5th 19 at 22:03
Build a path from __DIR__ for example php.net/manual/ru/language.constants.predefined.php - kenny commented on June 5th 19 at 22:06
, accusense - jaylon_Hahn74 commented on June 5th 19 at 22:09
June 5th 19 at 21:47
Solution
in the crown established a full path?
whereis php show what gives
php: /usr/bin/php /usr/bin/php7.0 /usr/lib/php /etc/php /usr/share/php7.0-xml /usr/share/php7.0-mysql /usr/share/php: /usr/share/php7.0-mbstring /usr/share/php7.0-imap /usr/share/php7.0-mcrypt /usr/share/php7.0-pspell /usr/share/php7.0-json /usr/share/php7.0-intl /usr/share/php7.0-common /usr/share/php7.0-ldap /usr/share/php7.0-gd /usr/share/php7.0-opcache /usr/share/php7.0-readline /usr/share/php7.0-soap /opt/php55/bin/php /usr/share/man/man1/php.1.gz - jaylon_Hahn74 commented on June 5th 19 at 21:50
way full, so tried to just execute in console /usr/bin/php /var/site.ru/script.php and writes:
PHP Warning: opendir(/form/uploads): failed to open dir: No such file or directory in /var/site.ru/script.php on line 5
php:
<?php
$dir=$_SERVER['DOCUMENT_ROOT']."/form/uploads"; //read this directory 
$todel=60; // time to delete 
if($OpenDir=opendir($dir)){
 while(($file=readdir($OpenDir)) !== false){ 
 if ($file != "." && $file != ".."){ 
 $dtime=intval(time()-filectime("{$dir}/{$file}")); 
 if ($dtime-->=$todel) {unlink("{$dir}/{$file}");} 
 } 
 } 
 closedir($OpenDir); 
}
- jaylon_Hahn74 commented on June 5th 19 at 21:53
turns out it is necessary instead of $_SERVER['DOCUMENT_ROOT'] something else to use? - kenny commented on June 5th 19 at 21:56
well prescribe full path
$dir=/var/www/..../..../form/uploads (or whatever you have)
start php7 so if you have:
*/05 * * * * /usr/bin/php -c /etc/php/7.0/fpm/php.ini -q -f /var/www/...../data/www/cron/cron_script.php >/dev/null 2>&1

Toko your ways to write - jaylon_Hahn74 commented on June 5th 19 at 21:59
June 5th 19 at 21:49
Solution
Try

*/5 * * * * cd /var/site.ru ; php script.php

And watch be sure that after this line there is at least one empty string — this is important because Kron believes the entire file invalid, if at the end there is no empty string

Find more questions by tags CronPHPUbuntu Server