LinuxQuestions.org
Visit Jeremy's Blog.
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 03-18-2011, 07:18 PM   #1
leonard_linuxquestions
LQ Newbie
 
Registered: Mar 2011
Posts: 2

Rep: Reputation: 0
No output to log file from daemon program


Hi, I am learning Linux daemon programming and write a simple daemon program. The issue is no data is written to the log file (/var/tmp/simpledeamon.log) though the file is successfully created (it's file size is zero from ls -l output). Could someone kindly point out the error in my program, Thanks.

The code is based on the Devin Watson's article at
http://www.netzmafia.de/skripten/uni...mon-howto.html. Here is the code:

// simplydaemon.cpp
// A simple Linux daemon.

#include <sys/types.h>
#include <sys/stat.h> // for umask
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char **argv)
{
pid_t pid, sid; // child process id and session id.
FILE *fp_log;

// Fork off the parent process.
pid = fork();
if ( pid < 0 )
exit(EXIT_FAILURE);

// The child is born, kill the parent.
if ( pid > 0 )
exit(EXIT_SUCCESS);

// Now enter the child space.

// Change the file mode mask
umask(0);

// Open the log file.
if ( (fp_log = fopen("/var/tmp/simplydaemon.log", "a")) == NULL ) {
fprintf(stderr, "simplydaemon: fail to open log file.\n");
exit(EXIT_FAILURE);
}

// Create a new SID for the child
sid = setsid();
if ( sid < 0 ) {
fprintf(fp_log, "simplydaemon: fail to create a new SID for the child.\n");
fclose(fp_log);
exit(EXIT_FAILURE);
}

// Change the current working directory
if ( chdir("/") < 0 ) {
fprintf(fp_log, "simplydaemon: fail to change the current working directory");
fclose(fp_log);
exit(EXIT_FAILURE);
}

// Close all file descriptors
//for ( int i = getdtablesize(); i>=0; --i )
// close(i);
// Close the standard file descriptors
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);

// The infinite loop
while (1) {
// The only work is to print some message to log file.
fprintf(fp_log, "simplydaemon: I am OK.\n");

// then sleep
sleep(60);
}

fclose(fp_log);
exit(EXIT_SUCCESS);
}
 
Old 03-18-2011, 10:08 PM   #2
MS3FGX
LQ Guru
 
Registered: Jan 2004
Location: NJ, USA
Distribution: Slackware, Debian
Posts: 5,852

Rep: Reputation: 361Reputation: 361Reputation: 361Reputation: 361
Taking a quick look, I think the problem is simply that the file changes haven't been written to disk yet. It can take time for file writes to actually be committed to disk, and since this is only adding a very little amount to the file every minute, these operations could be cached in RAM for quite some time.

Closing the file should force the pending changes to get written out, but your logic prevents the program from ever closing the file (this is a bad idea in itself, by the way).

If you want to see this output in real time, use the fflush() function on the stream.
 
Old 03-19-2011, 08:10 AM   #3
leonard_linuxquestions
LQ Newbie
 
Registered: Mar 2011
Posts: 2

Original Poster
Rep: Reputation: 0
MS3FGX, thanks. It works.
 
  


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
Output to log file BfJj Linux - Newbie 1 02-02-2011 02:43 PM
Daemon shell or perl script to monitor a log file khriz Programming 4 01-07-2010 07:35 AM
Clean Up Log - Search for Pattern in Log file and Output result bridrod Linux - Newbie 10 01-05-2010 09:49 AM
Possible to see a program output and backup its log at the same time? cdcshu Linux - Software 5 02-13-2008 05:26 AM
Log Screen Output to a file sdandeker Linux - Newbie 3 09-17-2003 02:57 AM

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

All times are GMT -5. The time now is 07:27 PM.

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