现在的位置: 首页 > 综合 > 正文

Linux下产生日志并定期删除

2013年09月12日 ⁄ 综合 ⁄ 共 843字 ⁄ 字号 评论关闭

只是写了一个框架 提取系统时间 写到文本 读者可以根据自己情况修改

#include <stdlib.h>
#include <stdio.h>
#include<string.h>
#include <time.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

void main()
{
    char logtime[40];
	char date[40];
	char buf[30];//用于生成目录名
	char dest[100];
	struct timeval tv;//获取纳秒级
    gettimeofday(&tv,0);
	int fd,len;

    time_t now_time;
    now_time=time(NULL);
	strftime(buf,sizeof(buf),"./log/20%y-%m-%d.log",localtime(&now_time));//以日子作为文件名

	strftime(logtime,sizeof(logtime),"日期:20%y-%m-%d 时间:%H:%M:%S",localtime(&now_time));		
	sprintf(date,"%s.%03d\n",logtime,tv.tv_usec/1000);//用于记录时间
	system("find /home/jiajunjie/log/ -atime +7 -exec rm {} '\;'");//-mtime +7七天前 -amin +1一分钟前
	sleep(3);
	fd=open(buf,O_CREAT| O_RDWR,0664);
	lseek(fd,0,SEEK_END);
		write(fd,date,strlen(date));
			printf("%s",date);
	close(fd);
}


 http://www.centos.bz/2011/05/time-to-delete-expired-files-linux/

抱歉!评论已关闭.