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

  • Home
  • SEARCH
  • 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 873735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:59:19+00:00 2026-05-15T10:59:19+00:00

I am developing a script to read and search log files for a big

  • 0

I am developing a script to read and search log files for a big system at work.
It currently works like this:

  1. User defines search parameters
  2. PHP uses shell_exec(“tac logfile.log > tmpfile.log”) to copy log file (and reverse the lines) to a temp file
  3. PHP fopen()’s the temp file
  4. PHP reads through each line of the file and applies the search parameters
  5. PHP deletes the temp file made in #2

I decided upon this method as the log files are written to every few seconds, and I need to read the log file backwards.

My problem is that step #2 takes a really long time when the log file is >300MB, and each day’s log file is easily 500MB, so searching this much data is really time consuming.

I have a couple of questions:

  1. Can I simply run fopen() on the active log file?
  2. Can it cause corruption of the log file if I do simply read it while other scripts are writing to it?
  3. Will it slow down the scripts writing to the log file if I am reading it at the same time?
  4. Can I tell PHP to read each line of the file backwards rather than forwards?

I hope that makes sense…

  • 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-05-15T10:59:19+00:00Added an answer on May 15, 2026 at 10:59 am

    To answer the numbered questions:

    On a Unix or Unix-like system:

    1: Feel free to fopen() anything you want. Other processes appending to the file won’t be screwed up. Other processes moving the file won’t be screwed up. Other processes deleting the file won’t be screwed up. Their operations will continue to succeed, as will yours.

    2: Reading the file won’t prevent writers from writing, unless the filesystem is applying mandatory file locking. This is very unlikely, as it is a large performance penalty. (See your system’s fcntl(2) manpage for details on mandatory locking.)

    3: The scripts writing to the logs will probably not run slower while you are reading the files. The file data and metadata will need to be brought into memory for writing or reading; the logging might even run (ever so slightly) faster if your search keeps the caches hot.

    4: Haven’t got a clue. 🙂

    I can think of several possible approaches to solving this problem:

    First, you could perform the search first and then reverse the results:

    grep danger /var/log/messages | tac
    

    The search won’t go any faster, but reversing will, assuming your results are a small-enough subset of the entirety.

    Second, you could break your log file into pieces periodically using dd(1)’s skip=BLOCKS parameter; by breaking the file into 10-megabyte chunks, reversing those once, and handling the remainder afresh each time, you can amortize the cost of reversing the file all throughout the day. Programming this correctly would require more effort, but the time savings might be worth it. It depends how often the queries need to be made.

    Third, you could write your own moral equivalent of tail(1) -f that is stuffing each new line into a ring-buffer file backwards. You’d be overwriting previous lines as you go, but it sounds like your log rotation already limits the amount of data being returned. A 600 megabyte limit might be reasonable. Your queries could start at the most-recently-written-byte, read through to the end of the file, and then start over at the beginning of the file if not enough results were returned. This would probably require the most effort to get correct, but amortizes the cost of reversing the database through every single log write. (If the scripts writing to the logs are bursty, this might seriously compete with the scripts for disk bandwidth at peak usage.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 434k
  • Answers 434k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The first is a standalone function; it's not tied to… May 15, 2026 at 3:14 pm
  • Editorial Team
    Editorial Team added an answer There are two approaches that I know of: Collect multiple… May 15, 2026 at 3:14 pm
  • Editorial Team
    Editorial Team added an answer I, too, am a single developer and use much the… May 15, 2026 at 3:14 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.