每天 23:30 調用應用程序
每間隔1000小時執行一次應用程序
[code]
#Makefile
CC = cc
COPS = -g -D_DEBUG
RTLIB = -lrt
all: mytimer
mytimer: mytimer.c
$(CC) $(COPS) -o mytimer mytimer.c $(RTLIB)
clean:
rm -f core *.o mytimer
[/code]
[code]
/* mytimer.c
*
* Usage: mytimer time interval
* Example: mytimer 23:30:00 1000
*
*/
#include
#include
#include
#include
#include
#include
#include
#define SIGNUM SIGALRM
#define SIGINFO1 1
#define SIGINFO2 2
#define DAYINTERVAL 24*60*60
#define BILLION 1000000000L
#define TIMER_MSG1 "Program 1\t"
#define TIMER_MSG2 "Program 2\t"
static void handler(int signo, siginfo_t *info, void *context) {
int errsave;
errsave = errno;
switch (info->si_value.sival_int) {
case SIGINFO1:
write(STDOUT_FILENO, TIMER_MSG1, sizeof(TIMER_MSG1) - 1);
system("/usr/bin/date +%X");
break;
case SIGINFO2:
write(STDOUT_FILENO, TIMER_MSG2, sizeof(TIMER_MSG2) - 1);
system("/usr/bin/date +%X");
break;
default:
break;
}
errno = errsave;
}
static int setsighandler(void) {
struct sigaction act;
act.sa_flags = SA_SIGINFO;
act.sa_sigaction = handler;
if ((sigemptyset(&act.sa_mask) == -1) ||
(sigaction(SIGNUM, &act, NULL) == -1))
return -1;
return 0;
}
static int settime(long val, long interval, int evt) {
timer_t timerid;
struct itimerspec value;
struct sigevent evp;
evp.sigev_notify = SIGEV_SIGNAL;
evp.sigev_signo = SIGNUM;
switch (evt) {
case SIGINFO1:
evp.sigev_value.sival_int = SIGINFO1;
break;
case SIGINFO2:
evp.sigev_value.sival_int = SIGINFO2;
break;
default:
return -1;
}
if (timer_create(CLOCK_REALTIME, &evp, &timerid) == -1) {
perror("Failed to create a timer");
return -1;
}
value.it_interval.tv_sec = (long)interval;
value.it_interval.tv_nsec = (interval - value.it_interval.tv_sec)*BILLION;
if (value.it_interval.tv_nsec >= BILLION) {
value.it_interval.tv_sec++;
value.it_interval.tv_nsec -= BILLION;
}
value.it_value.tv_sec = (long)val;
value.it_value.tv_nsec = (val - value.it_value.tv_sec)*BILLION;
if (value.it_value.tv_nsec >= BILLION) {
value.it_value.tv_sec++;
value.it_value.tv_nsec -= BILLION;
}
return timer_settime(timerid, 0, &value, NULL);
}
int string2time(const char *str, struct tm **time) {
const char *delimiters = ": \t";
char *buf;
char *bufp;
buf = (char *) malloc(strlen(str) + 1);
strcpy(buf, str);
*time = (struct tm *) malloc(sizeof(struct tm));
if ((bufp = strtok(buf, delimiters)) != NULL) {
(*time)->tm_hour = atoi(bufp);
if ((bufp = strtok(NULL, delimiters)) != NULL) {
(*time)->tm_min = atoi(bufp);
if ((bufp = strtok(NULL, delimiters)) != NULL)
(*time)->tm_sec = atoi(bufp);
else
(*time)->tm_sec = 0;
} else {
fprintf(stderr, "Time value(%s) is invalid\n", str);
free(buf);
return -1;
}
} else {
fprintf(stderr, "Time value(%s) is invalid\n", str);
free(buf);
return -1;
}
free(buf);
return 0;
}
void freetimepointer(struct tm **time) {
if (time == NULL)
return;
if (*time != NULL)
free(*time);
free(time);
}
int runtime(char *timep) {
time_t current;
time_t end;
struct tm *tcurrent;
struct tm *tend;
if (setsighandler() == -1) {
perror("Failed to setup signal handler");
return -1;
}
string2time(timep, &tend);
end = tend->tm_hour*60*60 + tend->tm_min*60 + tend->tm_sec;
current = time(NULL);
tcurrent = localtime(¤t);
current = tcurrent->tm_hour*60*60 + tcurrent->tm_min*60 + tcurrent->tm_sec;
if (current > end)
end += 24*60*60 - end;
else
end -= current;
#ifdef _DEBUG
if (settime(end, 2, SIGINFO1) == -1)
return -1;
#else
if (settime(end, DAYINTERVAL, SIGINFO1)== -1)
return -1;
#endif
freetimepointer(&tend);
return 0;
}
int runinterval(int interval) {
time_t secs;
if((secs = interval*60*60) <= 0) {
fprintf(stderr, "Time value(%d) is invalid\n", interval);
return -1;
}
if (setsighandler() == -1) {
perror("Failed to setup signal handler");
return -1;
}
#ifdef _DEBUG
secs = 6;
#endif
if (settime(1, secs, SIGINFO2) == -1)
return -1;
return 0;
}
int main(int argc, char **argv)
{
if (argc != 3) {
printf("Usage: %s time interval\n", argv[0]);
printf("Example: %s 23:30:00 1000\n", argv[0]);
return 1;
}
if (runtime(argv[1]) == -1)
return 1;
if (runinterval(atoi(argv[2])) == -1)
return 1;
for ( ; ; )
pause();
return 0;
}
[/code]