Need help understanding managing log output from a daemon
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Need help understanding managing log output from a daemon
I've written a domain (unix) socket server. The server logs to stderr. I also wrote a simple application which reads from stdin and generates rolling log files. Currently I had it working fine doing something like the following:
where 1.7 talks about how daemons should work. So I added the fork()/setsid()/fork()/chdir()/umask(). All seems to work fine but now the command above which used to work no longer works, or at least doesn't seem to work as I expect it to. When I execute it the shell is blocked for some reason. I assume it's the pipe that's causing the problem but not sure exactly why. Also, I'm wondering whether I need to put the rolling log file logic inside my daemon in order for it to work. Any ideas?
In my world a daemon is created following the same process described in that link and you'll also find a blog entry in my list of blogs pretty much mirroring that same procedure, but using C code.
My most important point here though:
Since you've written aggregator, then why not just use open() and close() or fopen() and fclose() and write your own logs to files? No need to channel stdout and stderr in this manner, instead create logging from within your program.
I'm actually "funny", meaning "odd" in that I really don't use the syslog utilities from Linux. Probably because I've worked in non-OS or limited RTOS environments and had to create all support functions on my own for lack of having library functions available. As a result, I create my own logging utilities and macros. All they are, are the equivalent of printf(format-string, ...) allowing variable arguments, but instead of sending the logged data to stdout/stderr, they place my resultant log data into specific files. And given large architectures, we've usually segregated log files per major functional blocks. All related to the common libraries, go to one log file, all related to a particular sub-system, go to a log file for that sub-system.
My aggregator program is written in c. The part about the script was just that I had been working on writing a daemon script, eg. start stop etc., for my daemon and ran upon the link which talks about how a daemon should be written. So I added many of those points to my daemon and then found that the logging was no longer working.
I can certainly add the rolling log logic to my daemon, but it seemed useful to have a tool which would do that from any stdin and thus could work for many applications.
Yes, sorry. I did see "I have been looking into writing a daemon script ..." and interpreted that incorrectly.
Advice still stands though. No need to use stdout/err, instead write log files from within your code. Or use the syslog library. From what I've heard, it's good and I know it's very much used by standard Linux application programmers. Just I've never practiced that, which is my personal shortcoming.
I do not know what did you really write, but in general a daemon will not write its stdout, but a logfile directly.
Also I do not really know what does it: "When I execute it the shell is blocked for some reason" really mean.
Did you try to use syslog and the "official" logrotate utility?
By "the shell being blocked" I mean that it's waiting on something. Normally, with the forks I added, the shell should return back and give me another prompt. However, it's blocked on something when I add my logging stuff (eg. the pipe to logrot).
I'm somewhat new to Linux though I'm aware that syslog is a popular logging mechanism. I will look into that some more.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.