Linux Linux program Exercise IX

Source: Internet
Author: User
Tags sprintf

Title: DIY A daemon that can run in the background when the console window is closed.
Every second to insert a record into the My.Log file, the record format is as follows: Yyyy-mm-dd hh:mi:se record content, yyyy for the year, MM for the month, DD for days, HH for hours, mi for minutes, se for seconds.
#ifdef __cplusplus extern " C " {}#endif// write log function //path: Log file name //msg: Log information  int writelog (constcharconst char * msg); #ifdef __ Cplusplus}#endif
#include <stdio.h>#include<stdlib.h>#include<string.h>#include<errno.h>#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include"mylog.h"intMainintArgChar*args[]) {pid_t PID=0; PID=Fork (); if(pid>0) {exit (0); }    if(pid==0) {setsid (); ChDir ("/"); Umask (0);        Close (Stdin_fileno);        Close (Stdout_fileno);        Close (Stderr_fileno); intI=0; Charbuf[ -]={0};  while(1) {Sleep (1); sprintf (BUF,"fly on air%d\n", i++); Writelog ("/home/test/1/testlog.txt", BUF); memset (BUF,0,sizeof(BUF)); }    }    return 0;}
#include <stdio.h>#include<stdlib.h>#include<string.h>#include<errno.h>#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include"mylog.h"intMainintArgChar*args[]) {pid_t PID=0; PID=Fork (); if(pid>0) {exit (0); }    if(pid==0) {setsid (); ChDir ("/"); Umask (0);        Close (Stdin_fileno);        Close (Stdout_fileno);        Close (Stderr_fileno); intI=0; Charbuf[ -]={0};  while(1) {Sleep (1); sprintf (BUF,"fly on air%d\n", i++); Writelog ("/home/test/1/testlog.txt", BUF); memset (BUF,0,sizeof(BUF)); }    }    return 0;}
. suffixes:.c. OCC=Gccsrcs=hello.cobjs=$ (srcs:.c=. O) EXEC=tecdstart:$ (OBJS)     -L.-lmylog-o $ (EXEC) $ (objs    )"^_^-----OK------^_^". C.O:     -wall-g-o [email protected]-C $< clean:    -F $ (    Objs)F $ (EXEC)

Linux Linux program Exercise IX

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.