skip to Main Content

I could use some assistance in making a PHP script that I could add to a cronjob that would include multiple, (10 to 15), commands such as:

line1: cat /dev/null > /var/www/vhosts/website.com
/logs/access_log.webstat  
line2: cat /dev/null > /var/www/vhosts/website.com/logs/big_access_log
line3: cat /dev/null > /var/log/plesk-roundcube/largefile.log

and so on. The commands work great from a command line, but doing this daily is time consuming and the files grow way too large even though they are being rotated. Any assistance would be greatly appreciated, thank you.

4

Answers


  1. Chosen as BEST ANSWER

    Thank you all for your assistance, the ending outcome is a awesome! 41 log files will no longer grow to gargantuan sizes. The implementation was as follows:

    PHP script written as such:

        <?php
        $output = shell_exec('cat /dev/null > /var/www/vhosts/website.com/logs/access_log.webstat');
        $output = shell_exec('cat /dev/null > /var/www/vhosts/website.com/logs/big_access_log');
        $output = shell_exec('cat /dev/null > /var/log/plesk-roundcube/largefile.log');
        ?>
    

    Then uploaded and set as a cron from the Plesk 12.5 panel. Tested and functioning beautifully!


  2. Could you possibly use the shell_exec command to complete these actions:

    Example:

    <?php
    $output = shell_exec('cat /dev/null > /var/www/vhosts/website.com /logs/access_log.webstat');
    echo "<pre>$output</pre>";
    ?>
    

    Then just create a cron job to run them at interval times.

    Login or Signup to reply.
  3. You can easily achieve the same result using native PHP code:

    // The list of files to truncate
    $listFiles = array(
        '/var/www/vhosts/website.com/logs/access_log.webstat',
        '/var/www/vhosts/website.com/logs/big_access_log',
        '/var/log/plesk-roundcube/largefile.log',
    );
    
    // Process all the files in the list
    foreach ($listFiles as $filename) {
        // Open the file for writing ('w')
        // Truncate it to zero length if it exists, create it if it doesn't exist
        $fh = fopen($filename, 'w');
        // Close the file; this commits the new file size to the disk
        fclose($fh);
    }
    
    Login or Signup to reply.
  4. It’s quite strange, by default this files should be rotated by psa-logrotate.
    Maybe something happens with logrotate package or crontask.

    Here is a default settings for rotating of domain’s logs:
    Plesk log rotate logrotate access.log error.log

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search