I am writing a program that is designed to never terminate, It will also produce large logs (files) of what it is doing. What I would like is for a linux process to periodically (every X minutes (cronjob?)) :

  1. Take the current logs from the current log file
  2. place them in a separate file (appropriately named) for offline storage / removal
  3. Truncate the current logfile so data is not duplicated.

All without data loss or interruption to the producing program. So that I end up with a sequence of time delimeted logs

Is there a Linux utility or known solution for this ? or should I build this logging functionality into the program ?

  • "without interruption" - do you mean you're not even allowed to send a signal to the process? – Mat Apr 11 '12 at 10:28
  • @Mat preferrably yes, but i will listen to other options! More generally i would like it to be "Without significant suspension or downtime" – NWS Apr 11 '12 at 10:32
  • Keep things simple, just output to stdout/err and have some process supervision software (e.g. systemd, Upstart, daemontools, runit,...) handle logging. – sr_ Apr 11 '12 at 10:48
  • I would prefer to avoid stdout etc... – NWS Apr 11 '12 at 11:54

Linux uses syslogd for managing log files. I've not used this personally, so I'm not sure what the gotchas might be, but essentially you just send messages to the log and the deamon takes care of the file rotation etc.

See here: http://linux.die.net/man/3/syslog

You would configure /etc/syslog.conf to route your messages to a particular file, and you can manually rotate the logs using logrotate, also, I think, or set up a cron job.

| improve this answer | |
  • This is a good answer because syslog is very flexible. There are multiple implementations, and you can (for example) divert certain classes of log messages to other systems (logging hosts) without having to re-architect your source program. – jmtd Apr 11 '12 at 10:51
  • This looks like the best approach, however if i understand the manpages correctly, am i limited to 8 LOG_LOCAL logs ? (In cas i want to run more than 8 processes using this logging feature!) – NWS Apr 11 '12 at 12:44
  • I believe your eight processes should use the same "facility" but different "ident" strings. You can then configure syslog.conf to do whatever you choose. Anyway, would it be a problem if all the messages got into the same file? Each line would be prefixed with the unique ident. – ams Apr 11 '12 at 12:48
  • Ah, no, I see what you mean. You can only select on the facilty, not the ident; I misunderstood that. Hmmm, that's not ideal if you want separate files. Looks like all your processes are expected to share the same file in this scheme. :( – ams Apr 11 '12 at 12:56
  • Thanks, It is the correct answer according to my Question. Ill probably use this for System (group of processes) level logging as it wont be a problem to have all of them in a log, but leave process specific logs for another mechanism. – NWS Apr 11 '12 at 13:04

Usually daemons just writes logs and nothing more. Log rotating is a job of another software. Usually it does logrotate or similar software.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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