#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#define RECORD_DEBUG
#define PATH "/root/timer_record"
#ifdef RECORD_DEBUG
#define TIME 60
#define DEADLINE (2)
#else
#define TIME 600 /* 10 min*/
#define DEADLINE (60*24*7*2) /* 2 WEEKS*/
#endif
static void sleep(int sec, int usec)
{
struct timeval tv;
tv.tv_sec = sec;
tv.tv_usec = usec;
select(0,NULL,NULL,NULL,&tv);
return;
}
int main()
{
FILE *fd;
int num;
int ret;
fd = fopen(PATH,"a+");
if (fd == NULL) {
printf("Can't open file/n");
exit(1);
}
if (fscanf(fd, "%d",&num) == EOF) {
num = 0;
ret = fprintf(fd, "%d", num);
fclose(fd);
}
while (1) {
if (num >= DEADLINE)
goto done;
sleep(TIME,0);
fd = fopen(PATH,"w");
if (fd == NULL)
goto done;
num ++;
printf("num = %d/n",num);
ret = fprintf(fd, "%d", num);
fclose(fd);
}
done:
system("source /opt/Qtopia/qpe.env");
system("/opt/at6600/usr/bin/shutdown");
return 0;
}
code depository: 2 week shutdown
本文介绍了一个简单的C语言程序,该程序实现了一个定时任务计数器。程序通过记录文件中的数值来跟踪时间进程,并在达到预设的时间界限后执行系统重启操作。此计数器可用于嵌入式系统或服务器中,确保按预定周期重启。

被折叠的 条评论
为什么被折叠?



