Skip to content

Commit

Permalink
a
Browse files Browse the repository at this point in the history
  • Loading branch information
buf1024 committed Apr 26, 2016
1 parent c3f18c6 commit 31d263a
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 56 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*~
myschd
*.txt
*.log
*.tmp
Expand Down
134 changes: 81 additions & 53 deletions mysch.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "iniconf.h"
#include "log.h"

//信号
//信号
static int g_term = 0;
static int g_usr1 = 0;
static int g_usr2 = 0;
Expand Down Expand Up @@ -49,17 +49,17 @@ static void sigchild(int signo)
g_chld = 1;
}

//注册信号
//注册信号
int reg_sign()
{
REGISTER_SIGNAL(SIGTERM, sigterm, 0);//Kill信号
REGISTER_SIGNAL(SIGINT, sigterm, 0);//终端CTRL-C信号
REGISTER_SIGNAL(SIGUSR1, sigusr1, 0);//SIGUSR1信号
REGISTER_SIGNAL(SIGUSR2, sigusr2, 0);//SIGUSR2信号
REGISTER_SIGNAL(SIGHUP, SIG_IGN, 0);//忽略SIGHUP信号
REGISTER_SIGNAL(SIGCHLD, sigchild, 0);//子进程退出
REGISTER_SIGNAL(SIGPIPE, SIG_IGN, 0);//忽略SIGPIPE信号
REGISTER_SIGNAL(SIGALRM, SIG_IGN, 0);//忽略SIGALRM信号
REGISTER_SIGNAL(SIGTERM, sigterm, 0);//Kill信号
REGISTER_SIGNAL(SIGINT, sigterm, 0);//终端CTRL-C信号
REGISTER_SIGNAL(SIGUSR1, sigusr1, 0);//SIGUSR1信号
REGISTER_SIGNAL(SIGUSR2, sigusr2, 0);//SIGUSR2信号
REGISTER_SIGNAL(SIGHUP, SIG_IGN, 0);//忽略SIGHUP信号
REGISTER_SIGNAL(SIGCHLD, sigchild, 0);//子进程退出
REGISTER_SIGNAL(SIGPIPE, SIG_IGN, 0);//忽略SIGPIPE信号
REGISTER_SIGNAL(SIGALRM, SIG_IGN, 0);//忽略SIGALRM信号

return 0;
}
Expand Down Expand Up @@ -223,11 +223,13 @@ int load_conf(sch_info_t* info, int flag)
char run_flag[256] = {0};
READ_CONF_STR_MUST(ssec, "RUN_FLAG", run_flag);
if(strcasecmp(run_flag, "forkfirst") == 0){
prog->flag = -1;
prog->flag = FORK_FIRST;
}else if(strcasecmp(run_flag, "forkonce") == 0){
prog->flag = 0;
prog->flag = FORK_ONCE;
}else if(strcasecmp(run_flag, "forkwatch") == 0){
prog->flag = 1;
prog->flag = FORK_WATCH;
}else if(strcasecmp(run_flag, "forkperiod") == 0){
prog->flag = FORK_PERIOD;
}else{
printf("syntax not right, available value: forkfirst forkonce forkwatch\n");
return -1;
Expand All @@ -245,7 +247,7 @@ int load_conf(sch_info_t* info, int flag)
prog->argc = ncmd;
strcpy(prog->cmd, prog->argv[0]);

if(prog->flag > 0) {
if(prog->flag) {
char cond[1024] = {0};
READ_CONF_STR_MUST(ssec, "RUN_TIME", cond);

Expand Down Expand Up @@ -317,7 +319,7 @@ int judge_condition(prog_t* prog)

int wday = tm->tm_wday;
if(wday == 0) {
wday = 7; // 星期天
wday = 7; // 星期天
}
int sec = tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec;

Expand All @@ -327,6 +329,13 @@ int judge_condition(prog_t* prog)
cond_t* c = list_node_value(node);

if(c->day[wday] == 1) {
if(prog->flag == FORK_PERIOD) {
int diff = time(NULL) - prog->time;
if(diff > c->start) {
list_release_iterator(it);
return 1;
}
}
if(sec >= c->start && sec <= c->end) {
list_release_iterator(it);
return 1;
Expand Down Expand Up @@ -380,33 +389,40 @@ int build_condition(prog_t* prog, const char* cond)
break;
}

char times[2][64] = { { 0 } };
int tnum = 0;
tnum = split(daytime[1], '-', (char**)times, 64, 2);
if(tnum != 2) {
LOG_ERROR("split %s failed.\n", daytimes[i]);
fail = 1;
break;
}
if(strlen(times[0]) != 5 || times[0][2] != ':' ||
strlen(times[1]) != 5 || times[1][2] != ':' ) {
LOG_ERROR("time split format not ritht. time1=%s, times2=%s\n",
times[0], times[1]);
fail = 1;
break;
}

int t_start = atoi(times[0]) * 3600 + atoi(times[0] + 3) * 60;
int t_end = atoi(times[1]) * 3600 + atoi(times[1] + 3) * 60;

if(t_start >= t_end ||
t_start < 0 || t_end < 0 ||
t_start > 86400 || t_end > 86400) {
LOG_ERROR("time data format not ritht. time1=%s, times2=%s, start=%d, end=%d\n",
times[0], times[1], t_start, t_end);
fail = 1;
break;
}
int t_start, t_end;
if (prog->flag != FORK_PERIOD) {
char times[2][64] = { { 0 } };
int tnum = 0;
tnum = split(daytime[1], '-', (char**) times, 64, 2);
if (tnum != 2) {
LOG_ERROR("split %s failed.\n", daytimes[i]);
fail = 1;
break;
}
if (strlen(times[0]) != 5 || times[0][2] != ':'
|| strlen(times[1]) != 5 || times[1][2] != ':') {
LOG_ERROR("time split format not ritht. time1=%s, times2=%s\n",
times[0], times[1]);
fail = 1;
break;
}

t_start = atoi(times[0]) * 3600 + atoi(times[0] + 3) * 60;
t_end = atoi(times[1]) * 3600 + atoi(times[1] + 3) * 60;

if (t_start >= t_end || t_start < 0 || t_end < 0 || t_start > 86400
|| t_end > 86400) {
LOG_ERROR(
"time data format not ritht. time1=%s, times2=%s, start=%d, end=%d\n",
times[0], times[1], t_start, t_end);
fail = 1;
break;
}

}else{
t_start = atoi(daytime[1]) * 60;
t_end = t_start;
}

LOG_DEBUG("condition: daystart=%d, dayend=%d, timestart=%d, timeend=%d\n",
d_start, d_end, t_start, t_end);
Expand Down Expand Up @@ -495,7 +511,7 @@ int run_prog(prog_t* prog)
time_t now = time(NULL);
if(prog->flag > 0 &&
prog->time != 0) {
// 防止死循环,常驻进程启动不成功需等待
// 防止死循环,常驻进程启动不成功需等待
time_t diff = now - prog->time;
if(diff <= g_info.sleep_time) {
LOG_INFO("pause start program %s, timediff %d\n",
Expand Down Expand Up @@ -561,7 +577,7 @@ int update_pid(prog_t* prog)
prog->pid = pid;
prog->update_pid = 1;

// 新进程信息
// 新进程信息

pid_t* dp = (pid_t*)malloc(sizeof(dp));
prog_t** dpp = (prog_t**)malloc(sizeof(dpp));
Expand Down Expand Up @@ -611,28 +627,40 @@ int mytask()
prog_t* p = list_node_value(n);
if(p->flag < 0) {
if(p->pid == 0) {
// 启动程序
// 启动程序
run_prog(p);
break;
}
if(!p->has_pid_file) {
if (kill(p->pid, 0) == 0) {
// 等待程序结束
// 等待程序结束
break;
}
}
// 程序已经结束,继续
// 程序已经结束,继续
list_del_node(g_info.progq, n, AL_FREE);

}else if(p->flag == 0) {
}else if(p->flag == FORK_ONCE || p->flag == FORK_PERIOD) {
if (p->flag == FORK_PERIOD) {
if (p->pid && kill(p->pid, 0) == 0) {
continue;
}
if (judge_condition(p)) {
run_prog(p);
// 程序已经启动,不管它生死存亡
list_del_node(g_info.progq, n, AL_FREE);
continue;
}

}
run_prog(p);
// 程序已经启动,不管它生死存亡
// 程序已经启动,不管它生死存亡
list_del_node(g_info.progq, n, AL_FREE);
}else {
prog_t* f = (prog_t*)
list_node_value(list_first(g_info.progq));
if(f->flag < 0) {
// 等待程序结束
// 等待程序结束
continue;
}

Expand All @@ -643,7 +671,7 @@ int mytask()
continue;
}
if (kill(p->pid, 0) == 0) {
// 程序正运行
// 程序正运行
continue;
}
}
Expand All @@ -655,7 +683,7 @@ int mytask()
continue;
}
if (kill(p->pid, 0) == 0) {
// 程序正运行,干掉它
// 程序正运行,干掉它
LOG_INFO("killing %d\n", p->pid);
kill(p->pid, SIGTERM);
waitpid(p->pid, NULL, 0);
Expand Down Expand Up @@ -685,7 +713,7 @@ int main(int argc, char* argv[])
int test = 0;
int daemon = 1;

//读取命令行参数
//读取命令行参数
while ((optch = getopt(argc, argv, optstring)) != -1) {
switch (optch) {
case 'h':
Expand Down
5 changes: 4 additions & 1 deletion mysch.conf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
# forkfirst 程序启动后先等待其运行完毕方可运行其它程序
# forkonce 程序启动后不管其运行状态
# forkwatch 程序按照RUN_TIME配置时间点运行,非该运行点自动停止通过kill信号
#RUN_TIME=1-5,23:30-16:30|1-5,17:30-21:30|6-7,00:00-8:30 RUN_FLAG=forkwatch时有效,程序运行时间段,星期(1-7),时间格式(24小时),各条件之间是或关系
# forkperiod 每隔一段时间运行一下
#RUN_TIME=1-5,23:30-16:30|1-5,17:30-21:30|6-7,00:00-8:30
# --RUN_FLAG=forkwatch时,程序运行时间段,星期(1-7),时间格式(24小时),各条件之间是或关系
# --RUN_FLAG=forkperiod时,格式1-7,30秒数
#
#RUN_HAS_PID_FILE=yes or no RUN_FLAG=forkwatch时有效
#RUN_PID_FILE=/home/heidong/project/dht/pid RUN_FLAG=forkwatch时有效,程序记录的PID,如果运行的程序没有该字段,最后不要用RUN_FLAG=1
Expand Down
10 changes: 8 additions & 2 deletions mysch_struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
" item: %s\n", k); \
return -1; \
}else{ \
/*必须确保V比T空间大*/ \
/*必须确保V比T空间大*/ \
strcpy(v, t); \
} \
}while(0)
Expand All @@ -61,11 +61,17 @@
do{ \
const char* t = ini_get_val(s, k); \
if(NULL != t){ \
/*必须确保V比T空间大*/ \
/*必须确保V比T空间大*/ \
strcpy(v, t); \
} \
}while(0)


#define FORK_FIRST (-1)
#define FORK_ONCE (0)
#define FORK_WATCH (1)
#define FORK_PERIOD (2)

typedef struct prog_s prog_t;
typedef struct cond_s cond_t;
typedef struct sch_info_s sch_info_t;
Expand Down

0 comments on commit 31d263a

Please sign in to comment.