Add configuration file options

This commit is contained in:
aixiao 2020-09-11 18:14:13 +08:00
parent e166d8a830
commit 55dfaa0cee

54
main.c
View File

@ -44,53 +44,73 @@ int get_process_pid(char *proces_name)
char *times() char *times()
{ {
time_t t; time_t t;
struct tm *timeinfo; //结构体 struct tm *timeinfo;
time(&t); time(&t);
timeinfo = localtime(&t); timeinfo = localtime(&t);
return asctime(timeinfo); //以字符串形式输出localtime本地时间 return asctime(timeinfo);
} }
int logs(char *str) int logs(char *str, char *configfile)
{ {
FILE *fp = NULL; FILE *fp = NULL;
fp = fopen(read_conf("daemon.conf", "global", "LOFFILE"), "a+"); fp = fopen(read_conf(configfile, "global", "LOFFILE"), "a+");
fprintf(fp, str); fprintf(fp, str);
return fclose(fp); return fclose(fp);
} }
int loop() int loop(char *configfile)
{ {
FILE *fp; FILE *fp;
char buffer[PATH_SIZE]; char buffer[PATH_SIZE];
while (1) { while (1) {
if (get_process_pid(read_conf("daemon.conf", "global", "PROCESS_NAME")) <= 0) { if (get_process_pid(read_conf(configfile, "global", "PROCESS_NAME")) <= 0) {
logs(times()); logs(times(), configfile);
logs("没有运行\n"); logs("没有运行\n", configfile);
fp = _popen(read_conf("daemon.conf", "global", "COMMAND"), "r"); fp = _popen(read_conf(configfile, "global", "COMMAND"), "r");
logs(times()); logs(times(), configfile);
logs("执行结果\n"); logs("执行结果\n", configfile);
while (fgets(buffer, sizeof(buffer), fp)) { while (fgets(buffer, sizeof(buffer), fp)) {
//printf("%s", buffer); //printf("%s", buffer);
logs(buffer); logs(buffer, configfile);
} }
_pclose(fp); _pclose(fp);
} else { } else {
logs(times()); logs(times(), configfile);
logs("运行\n"); logs("运行\n", configfile);
} }
sleep(atoi(read_conf("daemon.conf", "global", "TIME"))); sleep(atoi(read_conf(configfile, "global", "TIME")));
} }
} }
int main(int argc, char **argv) int main(int argc, char **argv, char **env)
{ {
char configfile[PATH_SIZE];
int opt;
memset(configfile, 0, PATH_SIZE);
while ((opt = getopt(argc, argv, "c:")) != -1) {
switch (opt) {
case 'c':
strcpy(configfile, optarg);
break;
default:
break;
}
}
if (strlen(configfile) == 0) {
strcpy(configfile, "daemon.conf");
}
printf("%s\n", configfile);
if (daemon(1, 1)) { if (daemon(1, 1)) {
perror("daemon"); perror("daemon");
return 1; return 1;
} }
loop(); loop(configfile);
return 0; return 0;
} }