Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
Turning a log file into a sort of circular buffer

I have a daemon application that prints to stdout/stderr its logs. I am looking for a way to keep the last N lines of log in memory, and access them on demand, ideally with something simple like cat. This logs don't have to be persisted to disk.

Basically I'd love to be able to do something like:

./ | memlog 100 mydaemonlog &

and then, afterwards:

cat /some/path/here/mydaemonlog

and I would get the last 100 lines of log, and then it would keep printing the log as it gets piped in the mydaemonlog file.

Is there some tool that is able to do what memlog is doing in the example above?

share|improve this question

marked as duplicate by MadHatter, Chris S Nov 20 '12 at 14:02

This question was marked as an exact duplicate of an existing question.

Is necessary to have N lines of log in memory? Or you want a solution to write the log to the disk ? – Sacx Nov 20 '12 at 11:59
@Sacx I don't need them persistent or on disk. I could write them on disk and the perform log rotation, but it seems hopelessly inelegant. – CAFxX Nov 20 '12 at 12:30
@MadHatter you're right. Flagged as duplicate. Thanks. – CAFxX Nov 20 '12 at 12:31

You might output logs to /dev/shm and then use tail to get last n lines.

./ >/dev/shm/log
tail -100 /dev/shm/log
share|improve this answer
Won't /dev/shm/log grow unbounded? – CAFxX Nov 20 '12 at 12:28
Yes, you'll need another script that monitors file size and truncates the file if necessary. – FINESEC Nov 20 '12 at 12:36

Have in-memory sqlite database. The script managing it can make sure only 100 rows are on there at a time.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.