LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-13-2016, 08:36 PM   #1
nickdu
Member
 
Registered: Aug 2015
Posts: 41

Rep: Reputation: Disabled
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:

./aggregator <some args> 2>&1 >/dev/null | ./logrot --daily ~/logs/agglog

aggregator is my daemon and logrot is the rolling log file tool.

I have been looking into writing a daemon script for my daemon and also found the following link:

http://www.faqs.org/faqs/unix-faq/programmer/faq/

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?

Thanks,
Nick
 
Old 04-14-2016, 06:24 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
How'd you accomplish fork() from within a script?

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.
 
Old 04-14-2016, 07:48 AM   #3
nickdu
Member
 
Registered: Aug 2015
Posts: 41

Original Poster
Rep: Reputation: Disabled
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.

Thanks,
Nick

Last edited by nickdu; 04-14-2016 at 07:52 AM.
 
Old 04-14-2016, 07:53 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
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.
 
1 members found this post helpful.
Old 04-14-2016, 07:56 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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?
 
1 members found this post helpful.
Old 04-14-2016, 08:30 AM   #6
nickdu
Member
 
Registered: Aug 2015
Posts: 41

Original Poster
Rep: Reputation: Disabled
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.

Thanks,
Nick
 
Old 04-15-2016, 01:57 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
without showing us what did you really coded we will not be able to help you further....
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Hoe to apache log managing in Bash script. jagafirst Linux - Server 1 11-07-2014 02:47 AM
No output to log file from daemon program leonard_linuxquestions Programming 2 03-19-2011 08:10 AM
LXer: Managing Swatch Output With Yet Another Perl Script LXer Syndicated Linux News 0 12-08-2008 05:41 AM
How to change Debian log rotation of syslog and daemon.log onmountain Linux - Newbie 2 07-31-2008 02:27 AM
Understanding /var/log/messages memory output kenneho Linux - Software 7 03-10-2008 10:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration