Sponsored Content
Top Forums Programming how to programing daemon to create log record every second Post 302606535 by slackman on Monday 12th of March 2012 01:36:08 AM
Old 03-12-2012
how to programing daemon to create log record every second

i write .....


Code:
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>

#define RUNNING_DIR     "/tmp"
#define LOCK_FILE       "exampled.lock"
#define LOG_FILE        "exampled.log"

void log_message(filename,message)
char *filename;
char *message;
{
FILE *logfile;
        logfile=fopen(filename,"a");
        if(!logfile) return;
        fprintf(logfile,"%s\n",message);
        fclose(logfile);
}

void signal_handler(sig)
int sig;
{
        switch(sig) {
        case SIGHUP:
                log_message(LOG_FILE,"hangup signal catched");
                break;
        case SIGTERM:
                log_message(LOG_FILE,"terminate signal catched");
                exit(0);
                break;
        }
}


void daemonize()
{
int i,lfp;
char str[10];
        if(getppid()==1) return; /* already a daemon */
        i=fork();
        if (i<0) exit(1); /* fork error */
        if (i>0) exit(0); /* parent exits */
        /* child (daemon) continues */
        setsid(); /* obtain a new process group */
        for (i=getdtablesize();i>=0;--i) close(i); /* close all descriptors */
        i=open("/dev/null",O_RDWR); dup(i); dup(i); /* handle standart I/O */
        umask(027); /* set newly created file permissions */
        chdir(RUNNING_DIR); /* change running directory */
        lfp=open(LOCK_FILE,O_RDWR|O_CREAT,0640);
        if (lfp<0) exit(1); /* can not open */
        if (lockf(lfp,F_TLOCK,0)<0) exit(0); /* can not lock */
        /* first instance continues */
        sprintf(str,"%d\n",getpid());
        write(lfp,str,strlen(str)); /* record pid to lockfile */

        signal(SIGCHLD,SIG_IGN); /* ignore child */
        signal(SIGTSTP,SIG_IGN); /* ignore tty signals */
        signal(SIGTTOU,SIG_IGN);
        signal(SIGTTIN,SIG_IGN);
        signal(SIGHUP,signal_handler); /* catch hangup signal */
        signal(SIGTERM,signal_handler); /* catch kill signal */

}


main()
{

        daemonize();
        while(1)
        sleep(1); /* run */
}



it's work for daemon and how to programing for this daemon create log record every second (exampled.log)

log record data like this....

Code:
1
2
3
4
5
6
7
8
9
10

and loop count infinity



thank you for advanced.

Last edited by pludi; 03-12-2012 at 06:16 AM..
 

10 More Discussions You Might Find Interesting

1. UNIX Desktop Questions & Answers

Graphics programing

Hi all! I`m new in Unix (Linux) and i whant to ask something! What language should i use for Linux developing.I meen applications an GAME DEVELOPING! Should i use C,TCL ??? Please help me on this ...:( (1 Reply)
Discussion started by: Sebastyan
1 Replies

2. Programming

log daemon

How does a log daemon work? If I'm upto write on, how should I design it? for example a syslog daemon, how should I make my daemon so that it gets things that hapends in the system? Or does every program have to call the daemon to log? please no answers about reading code, I'w tryed and... (0 Replies)
Discussion started by: Esaia
0 Replies

3. Shell Programming and Scripting

Removing Carriage return to create one record

I have a file with multiple records in it and want to create a single record by removing all the carriage returns, is there a sed command or another command that will easily allow this to happen. current layout 813209A 813273C 812272B expected result 813209A813273C812272B previously I... (3 Replies)
Discussion started by: r1500
3 Replies

4. UNIX for Advanced & Expert Users

How to see record for particular date from a log file

Hi Experts, I 'm a Oracle Dba, everytime if have to see the alert log file. I used tail command to see the last few line of the log file. I wished if i could see record for the particular date(e.g nov 23), here is sample log file. Wed Nov 28 21:43:59 2007 Control autobackup written to... (5 Replies)
Discussion started by: shaan_dmp
5 Replies

5. Shell Programming and Scripting

Awk Programing (need help)

plx help to solve these problems?? 1. Create a HERE document which will edit multiple files in the same directory, using the ed editor. I give you 3 original files: file1.c , file2.c , file3.c, download them and change each string "stdio.h" to "STDIO.H" in these files. Note: when execute the... (1 Reply)
Discussion started by: SoCalledEngr
1 Replies

6. UNIX for Dummies Questions & Answers

Create file with fixed record size

Hello all, Linux - Is there any way of creating a new file and determining its record size upon creation? open() and creat() do not refer to record size. Thanks... (2 Replies)
Discussion started by: klafte
2 Replies

7. Shell Programming and Scripting

shell programing...

Hi... i need to write a shell script wich shows the full name and station of every logged user in the system. pls help! (1 Reply)
Discussion started by: relu89
1 Replies

8. Programming

Unicode programing in C

im starting to go a little serious with c, woking in a personal project that will read a xml, which might contain Unicode characters (i know it will on my system, which is set to es_AR.UTF-8) im using mxml, and the documentation says it uses utf8 internally (no worries here). so i need to be... (4 Replies)
Discussion started by: broli
4 Replies

9. UNIX for Dummies Questions & Answers

How to Create a new internal DNS CNAME record

I need to create a new internal DNS CNAME record called "project.omc.eod" that points to a server called SPARC27.ds.eod. This is my first time doing this and any help would be greatly appreciated. Thanks in advance (10 Replies)
Discussion started by: mikeade
10 Replies

10. Linux

Corrupted daemon.log file

hi, yesterday I made something wrong and deleted daemon.log file. Then created a new file with the same name under the same path but now, it seems that file constructed has been corrupted . In earlier times, it was including regex name. Now there is no regex name. This is the current... (0 Replies)
Discussion started by: baris35
0 Replies
Appender::File(3pm)					User Contributed Perl Documentation				       Appender::File(3pm)

NAME
Log::Log4perl::Appender::File - Log to file SYNOPSIS
use Log::Log4perl::Appender::File; my $app = Log::Log4perl::Appender::File->new( filename => 'file.log', mode => 'append', autoflush => 1, umask => 0222, ); $file->log(message => "Log me "); DESCRIPTION
This is a simple appender for writing to a file. The "log()" method takes a single scalar. If a newline character should terminate the message, it has to be added explicitely. Upon destruction of the object, the filehandle to access the file is flushed and closed. If you want to switch over to a different logfile, use the "file_switch($newfile)" method which will first close the old file handle and then open a one to the new file specified. OPTIONS filename Name of the log file. mode Messages will be append to the file if $mode is set to the string "append". Will clobber the file if set to "clobber". If it is "pipe", the file will be understood as executable to pipe output to. Default mode is "append". autoflush "autoflush", if set to a true value, triggers flushing the data out to the file on every call to "log()". "autoflush" is on by default. syswrite "syswrite", if set to a true value, makes sure that the appender uses syswrite() instead of print() to log the message. "syswrite()" usually maps to the operating system's "write()" function and makes sure that no other process writes to the same log file while "write()" is busy. Might safe you from having to use other syncronisation measures like semaphores (see: Synchronized appender). umask Specifies the "umask" to use when creating the file, determining the file's permission settings. If set to 0222 (default), new files will be created with "rw-r--r--" permissions. If set to 0000, new files will be created with "rw-rw-rw-" permissions. owner If set, specifies that the owner of the newly created log file should be different from the effective user id of the running process. Only makes sense if the process is running as root. Both numerical user ids and user names are acceptable. group If set, specifies that the group of the newly created log file should be different from the effective group id of the running process. Only makes sense if the process is running as root. Both numerical group ids and group names are acceptable. utf8 If you're printing out Unicode strings, the output filehandle needs to be set into ":utf8" mode: my $app = Log::Log4perl::Appender::File->new( filename => 'file.log', mode => 'append', utf8 => 1, ); binmode To manipulate the output filehandle via "binmode()", use the binmode parameter: my $app = Log::Log4perl::Appender::File->new( filename => 'file.log', mode => 'append', binmode => ":utf8", ); A setting of ":utf8" for "binmode" is equivalent to specifying the "utf8" option (see above). recreate Normally, if a file appender logs to a file and the file gets moved to a different location (e.g. via "mv"), the appender's open file handle will automatically follow the file to the new location. This may be undesirable. When using an external logfile rotator, for example, the appender should create a new file under the old name and start logging into it. If the "recreate" option is set to a true value, "Log::Log4perl::Appender::File" will do exactly that. It defaults to false. Check the "recreate_check_interval" option for performance optimizations with this feature. recreate_check_interval In "recreate" mode, the appender has to continuously check if the file it is logging to is still in the same location. This check is fairly expensive, since it has to call "stat" on the file name and figure out if its inode has changed. Doing this with every call to "log" can be prohibitively expensive. Setting it to a positive integer value N will only check the file every N seconds. It defaults to 30. This obviously means that the appender will continue writing to a moved file until the next check occurs, in the worst case this will happen "recreate_check_interval" seconds after the file has been moved or deleted. If this is undesirable, setting "recreate_check_interval" to 0 will have the appender check the file with every call to "log()". recreate_check_signal In "recreate" mode, if this option is set to a signal name (e.g. "USR1"), the appender will recreate a missing logfile when it receives the signal. It uses less resources than constant polling. The usual limitation with perl's signal handling apply. Check the FAQ for using this option with the log rotating utility "newsyslog". recreate_pid_write The popular log rotating utility "newsyslog" expects a pid file in order to send the application a signal when its logs have been rotated. This option expects a path to a file where the pid of the currently running application gets written to. Check the FAQ for using this option with the log rotating utility "newsyslog". create_at_logtime The file appender typically creates its logfile in its constructor, i.e. at Log4perl "init()" time. This is desirable for most use cases, because it makes sure that file permission problems get detected right away, and not after days/weeks/months of operation when the appender suddenly needs to log something and fails because of a problem that was obvious at startup. However, there are rare use cases where the file shouldn't be created at Log4perl "init()" time, e.g. if the appender can't be used by the current user although it is defined in the configuration file. If you set "create_at_logtime" to a true value, the file appender will try to create the file at log time. Note that this setting lets permission problems sit undetected until log time, which might be undesirable. header_text If you want Log4perl to print a header into every newly opened (or re-opened) logfile, set "header_text" to either a string or a subroutine returning a string. If the message doesn't have a newline, a newline at the end of the header will be provided. Design and implementation of this module has been greatly inspired by Dave Rolsky's "Log::Dispatch" appender framework. COPYRIGHT AND LICENSE
Copyright 2002-2009 by Mike Schilli <m@perlmeister.com> and Kevin Goess <cpan@goess.org>. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.10.1 2010-07-21 Appender::File(3pm)
All times are GMT -4. The time now is 04:51 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy