how to programing daemon to create log record every second


 
Thread Tools Search this Thread
Top Forums Programming how to programing daemon to create log record every second
# 1  
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..
# 2  
Old 03-12-2012
Code:
static int  i;
main()
{
    char *buf;
    daemonize();
    while(1)
    {
        buf = malloc(10*sizeof(char));
    
        sprintf(buf,"%d",i);
        
        log_message(LOG_FILE,buf);

        free(buf);
    
        i++;
        
            sleep(1); /* run */
    }
}

# 3  
Old 03-13-2012
thank you very much
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question

Featured Tech Videos