Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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:

./mydaemon.sh | 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 has been asked before and already has an answer. If those answers do not fully address your question, please ask a new 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

2 Answers 2

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

./mydaemon.sh >/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.