1

I am new to using cron jobs, but I have studied the basics. I am able to call php using cron tab. When run manually, the code is executed. But when it is run from the cron job php can't read the log file.

$log = 'text.log';  
$saved = 10; 
$deleted_line = count(file($log))-$saved; 
$chk_size = filesize($log)/1024;
if($chk_size >= 2048) {
 $total_lines = file($log);  
    $final_lines = array_slice($total_lines, $deleted_line);
    $file = fopen('amrut-all-projects.log', 'w');
    $result = fwrite($file, implode('', $final_lines));
    fclose($file); 
    }  else { 
       echo 'Your File size less then 5kb.'; 
} 
Oliver Lorton
  • 709
  • 1
  • 6
  • 21
mahi bisht
  • 13
  • 3

3 Answers3

0

First of all please hit the CRON file manually using the browser. If the output came perfectly then don't worry about your code. Try to check the CRON settings.

  • Hi Fistley I Hit the manually cron file using the browser I got output . But when i am using cron file to run that time it cn't read log file . Please check this is my cron command . $file_ log = '/usr/local/bin/php /home/moud/public_html/amrut/schedular/test-amrut-all-projects.php '; – mahi bisht Jan 22 '20 at 11:16
  • Can you please tell me what should i check in CRON settings. – mahi bisht Jan 23 '20 at 10:48
0

@Amadan gave you a right hint. Below you have steps what to do:

ls -ld folder_u_try_to_save_to/

This will show you which user and group has access to your destination folder. If it's your_user then try to run cron with:

su -l your_user -c 'crontab -e'  

If it works correctly then check out this answer => How to specify in crontab by what user to run script?
on how to create cron tab for specific user and add this user to a group that has access to the folder you are trying to save data to.

  • Hi Fistley I Hit the manually cron file using the browser I got output . But when i am using cron file to run that time it cn't read log file . Please check this is my cron command . $file_ log = '/usr/local/bin/php /home/moud/public_html/amrut/schedular/test-amrut-all-projects.php '; – mahi bisht Jan 22 '20 at 11:16
  • If it's working via browser then you should run it as www-data user. I see you are calling it directly, try this: sudo -u www-data /usr/local/bin/php /home/moud/public_html/amrut/schedular/test-amrut-all-projects.php Obviously you need to have sudo access in sudoers. – Wojtek vanDer Jan 22 '20 at 13:14
  • Hi can i use this command ($file_log = -u www-data /usr/local/bin/php /home/moud/public_html/amrut/schedular/amrut-all-projects.log) if i am using this command then i am not access the file data please help us. $file_log = 'www-data/usr/local/bin/php/home/moud/public_html/amrut/schedular/amrut-all-projects.log'; $res = file($file_log); echo '
    '; print_r($res);die;
    – mahi bisht Jan 23 '20 at 10:44
  • response please. – mahi bisht Jan 24 '20 at 05:07
  • what is the output of 'ls -al amrut-all-projects.log' ? – Wojtek vanDer Jan 24 '20 at 10:39
  • Basically i want to save 200 line of code in log file (amrut-all-projects.log'). If Hit the manually cron file using the browser I got output [ Note ] If i am using cron that time cron job is not read the file path how can i mention log file path please tell me . My code is this pls see – mahi bisht Jan 24 '20 at 11:38
  • $log = 'amrut-all-projects.log'; $saved = 200; $deleted_line = count(file($log))-$saved; $chk_size = filesize($log)/1024; if($chk_size >= 2048) { $total_lines = file($log); $final_lines = array_slice($total_lines, $deleted_line); $file = fopen('amrut-all-projects.log', 'w'); $result = fwrite($file, implode('', $final_lines)); fclose($file); } else { echo 'Your File size less then 5mb.'; } – mahi bisht Jan 24 '20 at 11:38
0

Maybe you are specifying the wrong path.

Try this

/path/to/php -f /path/to/script.php >> /path/to/logfile.log
Abhishek Mishra
  • 340
  • 4
  • 14