2 * snooze - run a command at a particular time
4 * To the extent possible under law, Leah Neukirchen <leah@vuxu.org>
5 * has waived all copyright and related or neighboring rights to this work.
6 * http://creativecommons.org/publicdomain/zero/1.0/
10 #include <sys/types.h>
25 static long slack = 60;
26 #define SLEEP_PHASE 300
27 static int nflag, vflag;
29 static int timewait = -1;
30 static int randdelay = 0;
31 static char *timefile;
33 static sig_atomic_t alarm_rang = 0;
43 parse_int(char **s, size_t minn, size_t maxn)
49 n = strtol(*s, &end, 10);
54 if (n < (long)minn || n >= (long)maxn) {
55 fprintf(stderr, "number outside %zd <= n < %zd\n", minn, maxn);
69 n = strtol(s, &end, 10);
75 fprintf(stderr, "negative duration\n");
79 case 'm': n *= 60; break;
80 case 'h': n *= 60*60; break;
81 case 'd': n *= 24*60*60; break;
84 fprintf(stderr, "junk after duration: %s\n", end);
91 parse(char *expr, char *buf, long bufsiz, int offset)
94 long i, n = 0, n0 = 0;
96 memset(buf, ' ', bufsiz);
101 case '0': case '1': case '2': case '3': case '4':
102 case '5': case '6': case '7': case '8': case '9':
103 n = parse_int(&s, -offset, bufsiz);
109 n = parse_int(&s, -offset, bufsiz);
110 for (i = n0; i <= n; i++)
117 n = parse_int(&s, -offset, bufsiz);
120 for (i = n0; i < bufsiz; i += n)
130 for (i = 0; i < bufsiz; i++)
134 fprintf(stderr, "can't parse: %s %s\n", expr, s);
142 char weekday[8] = {0};
143 char dayofmonth[32] = {0};
144 char month[13] = {0};
145 char dayofyear[367] = {0};
146 char weekofyear[54] = {0};
148 char minute[60] = {0};
149 char second[61] = {0};
152 isoweek(struct tm *tm)
154 /* ugh, but easier than the correct formula... */
157 strftime(weekstr, sizeof weekstr, "%V", tm);
158 return parse_int(&w, 1, 54);
162 find_next(time_t from)
172 weekday[tm->tm_wday] == '*' &&
173 dayofmonth[tm->tm_mday-1] == '*' &&
174 month[tm->tm_mon] == '*' &&
175 weekofyear[isoweek(tm)-1] == '*' &&
176 dayofyear[tm->tm_yday] == '*')) {
177 if (month[tm->tm_mon] != '*') {
178 // if month is not good, step month
193 if (t > from+(366*24*60*60)) // no result within a year
197 int y = tm->tm_yday; // save yday
200 hour[tm->tm_hour] == '*' &&
201 minute[tm->tm_min] == '*' &&
202 second[tm->tm_sec] == '*')) {
203 if (hour[tm->tm_hour] != '*') {
207 } else if (minute[tm->tm_min] != '*') {
214 if (tm->tm_yday != y) // hit a different day, retry...
221 static char isobuf[25];
223 isotime(const struct tm *tm)
225 strftime(isobuf, sizeof isobuf, "%FT%T%z", tm);
230 main(int argc, char *argv[])
234 time_t now = time(0);
237 /* default: every day at 00:00:00 */
238 memset(weekday, '*', sizeof weekday);
239 memset(dayofmonth, '*', sizeof dayofmonth);
240 memset(month, '*', sizeof month);
241 memset(dayofyear, '*', sizeof dayofyear);
242 memset(weekofyear, '*', sizeof weekofyear);
247 setvbuf(stdout, 0, _IOLBF, 0);
249 while ((c = getopt(argc, argv, "+D:W:H:M:S:T:R:d:m:ns:t:vw:")) != -1)
251 case 'D': parse(optarg, dayofyear, sizeof dayofyear, -1); break;
252 case 'W': parse(optarg, weekofyear, sizeof weekofyear, -1); break;
253 case 'H': parse(optarg, hour, sizeof hour, 0); break;
254 case 'M': parse(optarg, minute, sizeof minute, 0); break;
255 case 'S': parse(optarg, second, sizeof second, 0); break;
256 case 'd': parse(optarg, dayofmonth, sizeof dayofmonth, -1); break;
257 case 'm': parse(optarg, month, sizeof month, -1); break;
258 case 'w': parse(optarg, weekday, sizeof weekday, 0);
259 // special case: sunday is both 0 and 7.
260 if (weekday[7] == '*')
263 case 'n': nflag++; break;
264 case 'v': vflag++; break;
265 case 's': slack = parse_dur(optarg); break;
266 case 'T': timewait = parse_dur(optarg); break;
267 case 't': timefile = optarg; break;
268 case 'R': randdelay = parse_dur(optarg); break;
270 fprintf(stderr, "Usage: %s [-nv] [-t timefile] [-T timewait] [-R randdelay] [-s slack]\n"
271 " [-d mday] [-m mon] [-w wday] [-D yday] [-W yweek] [-H hour] [-M min] [-S sec] COMMAND...\n"
272 "Timespec: exact: 1,3,5\n"
274 " every n-th: /10\n", argv[0]);
278 time_t start = now + 1;
282 if (stat(timefile, &st) < 0) {
285 t = start - slack - 1 - timewait;
289 if (timewait == -1) {
290 while (t < start - slack)
291 t = find_next(t + 1);
294 if (t + timewait > start)
295 start = st.st_mtime + timewait;
302 long rnd = getauxval(AT_RANDOM);
304 delay = rnd % randdelay;
308 srand48(getpid() ^ start);
309 delay = lrand48() % randdelay;
312 printf("randomly delaying by %lds.\n", delay);
316 t = find_next(start);
318 fprintf(stderr, "no satisfying date found within a year.\n");
323 /* dry-run, just output the next 5 dates. */
325 for (i = 0; i < 5; i++) {
327 struct tm *tm = localtime(&t);
328 strftime(weekstr, sizeof weekstr, "%a", tm);
329 printf("%s %s %2ldd%3ldh%3ldm%3lds\n",
332 ((t - now) / (60*60*24)),
333 ((t - now) / (60*60)) % 24,
334 ((t - now) / 60) % 60,
336 t = find_next(t + 1);
339 "no satisfying date found within a year.\n");
346 struct tm *tm = localtime(&t);
348 printf("Snoozing until %s\n", isotime(tm));
350 // setup SIGALRM handler to force early execution
352 sa.sa_handler = &wakeup;
353 sa.sa_flags = SA_RESTART;
354 sigfillset(&sa.sa_mask);
355 sigaction(SIGALRM, &sa, NULL); // XXX error handling
357 while (!alarm_rang) {
362 printf("Time moved backwards, rescheduled for %s\n", isotime(tm));
366 if (now - t <= slack) // still about time
368 else { // reschedule to next event
370 printf("Missed execution at %s\n", isobuf);
371 t = find_next(now + 1);
374 printf("Snoozing until %s\n", isotime(tm));
377 // do some sleeping, but not more than SLEEP_PHASE
380 ts.tv_sec = t - now > SLEEP_PHASE ? SLEEP_PHASE : t - now;
383 // we just iterate again when this exits early
387 // no command to run, the outside script can go on
393 tm = localtime(&now);
394 printf("Starting execution at %s\n", isotime(tm));
397 execvp(argv[optind], argv+optind);