定时工作小程序

  • Posted on
  • by

虽然用unix下面的at和cron命令可以实现定时执行任务的目的,但是这个程序需要管理员的权限才可以使用。

由于工作中的题都要算很久,因此我需要修改原来的一个简单的定时工作的程序以实现可以指定在某个时候执行某一个命令,并且可以指定程序是每天执行一次还是只是到了指定时间执行完就退出。

我于是寻找源代码,虽然linux是开源的,但是实在找不着源代码的位置。经过了多次修改,原来的代码也被改得面目全非了,放在这里当作备份(如果不熟悉C语言,请慎用,根据实际需要编写,对于不同的情况,可能会有BUG)

通过练习,我的心得就是:

会用一种比较高级的语言,如C进行编程的能力是要有的,但是如果要以编程为生真不是一件幸事。


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main(int argc, char** argv)
{
// command manual
if(argc < 2) {
printf("Usage: %s [OPTION] \"command1[;command2][;command3][..]\"\n",argv[0]);
printf("Description:\n");
printf("\tExecute a command at a time define by the user.Press ctrl+z to quit.Then enter: ps to find the PID and kill the PID.Please notice:When there are more than 1 command to execute, please include them in a pair of double quation marks(\"\")\n");
printf("Options:\n");
printf("\t-t TIME\n");
printf("\t\tdefine the time, format of TIME must be:HH:MM. default time is 5:59 \n");
printf("\t-n 0|1\n");
printf("\t\tdefine the execution times of the command.0 check every day and do not quit, 1 only execute one time. Default=0\n");
printf("\t-iw DAY\n");
printf("\t\tThe day to idel the work, format of DAY must be one of:Mon,Tue,Wed,Thu,Fri,Sat,Sun.No a day will be idel in the default setting\n");
printf("Example:\n");
printf("\t%s -t 5:59 -n 0 -iw Sat \"ls ..;ls \"\n",argv[0]);
exit(0);
}

//check the user's command
char cmdstr[BUFSIZ];
int cmdlen=0;
int i;
for(i=1;i<argc;i++) {
cmdlen=strlen(argv[i]);
cmdlen ++;
}
if(cmdlen > BUFSIZ-10) {
printf("***Error: comand_string is too long (>%d)\n",BUFSIZ-10);
exit(0);
}

//get information from user command
char target_time[BUFSIZ]="5:59";
int loop_time=0;
char idel_day[BUFSIZ]="NaN";
char NaN[BUFSIZ]="NaN";
int argc_no=1;
char string1[]="-t";
char string2[]="-n";
char string3[]="-iw";
char aweek[BUFSIZ]="Week:Mon,Tue,Wed,Thu,Fri,Sat,Sun";
struct tm tm2;
for(i=1;i<argc;i++) {
if(!strcmp(argv[i-1],string1)){
sscanf(argv[i], "%d:%d",&(tm2.tm_hour),&(tm2.tm_min));
if(tm2.tm_hour>23||tm2.tm_hour<0){
printf("ERROR:The hour should between 0~23 but now %d\n",tm2.tm_hour);
exit(0);
}
if(tm2.tm_min>59||tm2.tm_min<0){
printf("ERROR:The minute should between 0~59 but now %d\n",tm2.tm_min);
exit(0);
}
sprintf(target_time, argv[i]);
argc_no=argc_no+2;
i++;
continue;
}
else if(!strcmp(argv[i-1],string2)){
sscanf(argv[i],"%d",&loop_time );
if(!((loop_time == 0) || (loop_time == 1))){
printf("\nERROR:loop time should be 0 or 1 but now %d\n",loop_time);
exit(0);
}
argc_no=argc_no+2;
i++;
continue;
}
else if(!strcmp(argv[i-1],string3)){
sscanf(argv[i],"%s",idel_day );
for(int k=1;k<8;k++){
if (!strstr(aweek,idel_day)){
printf("ERROR:The date format is incorrect,should be one of %s but now %s",aweek,idel_day);
exit(0);
}
}
argc_no=argc_no+2;
i++;
continue;
}

}

//show the command to be excuted
printf("target_time is %s\n",target_time);
printf("loop_time is %d\n",loop_time);
printf("idel day is %s\n",idel_day);
cmdstr[0]='\0';
for(argc_no;argc_no<argc;argc_no++) {
sprintf(&cmdstr[strlen(cmdstr)],"%s ",argv[argc_no]);
}
cmdstr[strlen(cmdstr)]='\0';
printf("\nuser command string=%s\n",cmdstr);

//excute the command if it's not weekend and the time is match.
sscanf(target_time, "%d:%d",&(tm2.tm_hour),&(tm2.tm_min));
while((loop_time ==0)||(loop_time == 1)){
while(1){
time_t t;
struct tm *tm1;
char buf[BUFSIZ];
t=time(NULL);
tm1=localtime(&t);
strftime(buf,sizeof(buf),"%c\n",tm1);
system("sleep 5");
system("date");
if ((tm2.tm_hour==tm1->tm_hour)&&(tm2.tm_min==tm1->tm_min)) {
if(((!strstr(NaN,idel_day)))&&(strstr(buf,idel_day))){
system("echo idel");
continue;
}
else if(strstr(NaN,idel_day)){
system("echo no idel. The command will be exectued in 60s.");
system("sleep 60");
break;
}
}
}
printf("now begin working in \'%s\'...\n\n",cmdstr);
system(cmdstr);
printf("\nnow end work.\n");
if(loop_time == 1)
break;
}
return 0;
}