Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8034409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:00:12+00:00 2026-06-05T02:00:12+00:00

I have a php script which runs on my ubunutu server. this script runs

  • 0

I have a php script which runs on my ubunutu server. this script runs in an infinite loop running queries and gathering statistics… it is a monitor for some other services. Every 60 seconds the stats that are gathered are dumped as a json object to a .js file. The .js file is monitored elsewhere and is unimportant for the purpose of my question.

When I run my script I redirect both stdout and stderr to a log file.
For the most part there is no output either standard or error and so the log will remain empty.

Obviously though on occasion there will be output. The problem I experienced last night was the script failed for some reason and the log file ended up filling up the whole partition and messed with the stability of the server.

What I have done.. or tried to do is add in a simple check that every 60 seconds (when the stats are dumped) the script checks the size of the log file.. and if it is greater than X mb in size, basically empties the file and allow logging to continue. With the intention being that if the script does experience a problem again, the log file wont fill up the whole partition.

I can successfully detect the log file size, I can successfully clear it when it reaches my set size, but then the next time there is either a STDOUT or STDERR write, all the previous data is dumped in one go.

Any assistance with this would be greatly appreciated.


Calling script by:

php checker.php &> log

but have tried

php checker.php > log 2>&1

and also redirecting the streams at the start of the script

close(STDOUT);
fclose(STDERR);
$STDOUT = fopen($LOG_FILE_LOCATION, 'wb');
$STDERR = fopen($LOG_FILE_LOCATION, 'wb');

each way produces the same output.

Code clearing log file

function CheckLogFilesize($file)
{
    global $LOG_FILE_LOCATION;
    global $MAX_LOG_SIZE_MB;
    $max_log_size_bytes = $MAX_LOG_SIZE_MB * 1024 * 1024;

    $size = exec('stat -c %s ' . $file);

    // If log file size is too large.. reset it.
    if (intval($size) > $max_log_size_bytes)
    {
        flush();

        // Clear the log
        $fp = fopen($file, 'w');
        fwrite($fp, '');
        fclose($fp);
    }
}

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T02:00:15+00:00Added an answer on June 5, 2026 at 2:00 am

    EDIT

    Figured it out:

    function get_log_size() {
        clearstatcache();
        return filesize("/tmp/log.log");
    }
    
    foreach (range(1, 1000000) as $interval) {
        echo $interval . "\n";
        usleep(10000);
        if ($interval % 50 == 0) {
            fwrite(STDERR, "mod 50 fail test");
        }
        $size = get_log_size();
        echo $size . " bytes\n";
        if ($size > 100) {
            $file = "/tmp/log.log";
            $fp = fopen($file, 'w');
            fclose($fp);
            print "Truncated\n";
        }
    
    }
    

    Started with

    php test.php >> /tmp/log.log 2>&1
    

    Note the >> instead of >. You have to use append mode

    Example output of tail -f /tmp/log.log:

    λ ~/ tail -f /tmp/log.log
    
    1
    2 bytes
    2
    12 bytes
    3
    23 bytes
    4
    34 bytes
    5
    45 bytes
    6
    56 bytes
    7
    67 bytes
    8
    78 bytes
    9
    89 bytes
    10
    101 bytes
    Truncated
    11
    13 bytes
    12
    25 bytes
    13
    37 bytes
    14
    49 bytes
    15
    61 bytes
    16
    73 bytes
    17
    85 bytes
    18
    97 bytes
    19
    109 bytes
    Truncated
    20
    13 bytes
    21
    25 bytes
    22
    37 bytes
    23
    49 bytes
    24
    61 bytes
    25
    73 bytes
    26
    85 bytes
    27
    97 bytes
    28
    109 bytes
    Truncated
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a scheduled task on my server which runs a PHP script every
I have this PHP script which i'm grabbing images from a directory and displaying
I have installed my PHP scripts which runs over Apache server in a Windows
I have a long running PHP script that has a memory leak which causes
I have tried a session.php script which runs at the head of each page
i have a php script which runs fine when executed by SSHing into my
I have a PHP script that runs as a background process. This script simply
I have a (PHP) script which runs on a new row in MySQL. However,
I have a PHP script which runs a MySQL query. $query = INSERT INTO
I have a php script which runs continuously several hours and every few seconds

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.