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 int jitter = 0;
32 static char *timefile;
34 static sig_atomic_t alarm_rang = 0;
44 parse_int(char **s, size_t minn, size_t maxn)
50 n = strtol(*s, &end, 10);
55 if (n < (long)minn || n >= (long)maxn) {
56 fprintf(stderr, "number outside %zd <= n < %zd\n", minn, maxn);
70 n = strtol(s, &end, 10);
76 fprintf(stderr, "negative duration\n");
80 case 'm': n *= 60; break;
81 case 'h': n *= 60*60; break;
82 case 'd': n *= 24*60*60; break;
85 fprintf(stderr, "junk after duration: %s\n", end);
92 parse(char *expr, char *buf, long bufsiz, int offset)
95 long i, n = 0, n0 = 0;
97 memset(buf, ' ', bufsiz);
102 case '0': case '1': case '2': case '3': case '4':
103 case '5': case '6': case '7': case '8': case '9':
104 n = parse_int(&s, -offset, bufsiz);
110 n = parse_int(&s, -offset, bufsiz);
111 for (i = n0; i <= n; i++)
118 n = parse_int(&s, -offset, bufsiz);
121 for (i = n0; i < bufsiz; i += n)
131 for (i = 0; i < bufsiz; i++)
135 fprintf(stderr, "can't parse: %s %s\n", expr, s);
143 char weekday[8] = {0};
144 char dayofmonth[32] = {0};
145 char month[13] = {0};
146 char dayofyear[367] = {0};
147 char weekofyear[54] = {0};
149 char minute[60] = {0};
150 char second[61] = {0};
153 isoweek(struct tm *tm)
155 /* ugh, but easier than the correct formula... */
158 strftime(weekstr, sizeof weekstr, "%V", tm);
159 return parse_int(&w, 1, 54);
163 find_next(time_t from)
173 weekday[tm->tm_wday] == '*' &&
174 dayofmonth[tm->tm_mday-1] == '*' &&
175 month[tm->tm_mon] == '*' &&
176 weekofyear[isoweek(tm)-1] == '*' &&
177 dayofyear[tm->tm_yday] == '*')) {
178 if (month[tm->tm_mon] != '*') {
179 // if month is not good, step month
194 if (t > from+(366*24*60*60)) // no result within a year
198 int y = tm->tm_yday; // save yday
201 hour[tm->tm_hour] == '*' &&
202 minute[tm->tm_min] == '*' &&
203 second[tm->tm_sec] == '*')) {
204 if (hour[tm->tm_hour] != '*') {
208 } else if (minute[tm->tm_min] != '*') {
215 if (tm->tm_yday != y) // hit a different day, retry...
219 if (jitter && !nflag) {
221 delay = lrand48() % jitter;
223 printf("adding %lds for jitter.\n", delay);
230 static char isobuf[25];
232 isotime(const struct tm *tm)
234 strftime(isobuf, sizeof isobuf, "%FT%T%z", tm);
239 main(int argc, char *argv[])
243 time_t now = time(0);
246 /* default: every day at 00:00:00 */
247 memset(weekday, '*', sizeof weekday);
248 memset(dayofmonth, '*', sizeof dayofmonth);
249 memset(month, '*', sizeof month);
250 memset(dayofyear, '*', sizeof dayofyear);
251 memset(weekofyear, '*', sizeof weekofyear);
256 setvbuf(stdout, 0, _IOLBF, 0);
258 while ((c = getopt(argc, argv, "+D:W:H:M:S:T:R:J:d:m:ns:t:vw:")) != -1)
260 case 'D': parse(optarg, dayofyear, sizeof dayofyear, -1); break;
261 case 'W': parse(optarg, weekofyear, sizeof weekofyear, -1); break;
262 case 'H': parse(optarg, hour, sizeof hour, 0); break;
263 case 'M': parse(optarg, minute, sizeof minute, 0); break;
264 case 'S': parse(optarg, second, sizeof second, 0); break;
265 case 'd': parse(optarg, dayofmonth, sizeof dayofmonth, -1); break;
266 case 'm': parse(optarg, month, sizeof month, -1); break;
267 case 'w': parse(optarg, weekday, sizeof weekday, 0);
268 // special case: sunday is both 0 and 7.
269 if (weekday[7] == '*')
272 case 'n': nflag++; break;
273 case 'v': vflag++; break;
274 case 's': slack = parse_dur(optarg); break;
275 case 'T': timewait = parse_dur(optarg); break;
276 case 't': timefile = optarg; break;
277 case 'R': randdelay = parse_dur(optarg); break;
278 case 'J': jitter = parse_dur(optarg); break;
280 fprintf(stderr, "Usage: %s [-nv] [-t timefile] [-T timewait] [-R randdelay] [-J jitter] [-s slack]\n"
281 " [-d mday] [-m mon] [-w wday] [-D yday] [-W yweek] [-H hour] [-M min] [-S sec] COMMAND...\n"
282 "Timespec: exact: 1,3,5\n"
284 " every n-th: /10\n", argv[0]);
288 time_t start = now + 1;
292 if (stat(timefile, &st) < 0) {
295 t = start - slack - 1 - timewait;
299 if (timewait == -1) {
300 while (t < start - slack)
301 t = find_next(t + 1);
304 if (t + timewait > start)
305 start = st.st_mtime + timewait;
309 srand48(getpid() ^ start);
313 delay = lrand48() % randdelay;
315 printf("randomly delaying by %lds.\n", delay);
319 t = find_next(start);
321 fprintf(stderr, "no satisfying date found within a year.\n");
326 /* dry-run, just output the next 5 dates. */
328 for (i = 0; i < 5; i++) {
330 struct tm *tm = localtime(&t);
331 strftime(weekstr, sizeof weekstr, "%a", tm);
332 printf("%s %s %2ldd%3ldh%3ldm%3lds ",
335 ((t - now) / (60*60*24)),
336 ((t - now) / (60*60)) % 24,
337 ((t - now) / 60) % 60,
340 printf("(plus up to %ds for jitter)\n", jitter);
344 t = find_next(t + 1);
347 "no satisfying date found within a year.\n");
354 struct tm *tm = localtime(&t);
356 printf("Snoozing until %s\n", isotime(tm));
358 // setup SIGALRM handler to force early execution
360 sa.sa_handler = &wakeup;
361 sa.sa_flags = SA_RESTART;
362 sigfillset(&sa.sa_mask);
363 sigaction(SIGALRM, &sa, NULL); // XXX error handling
365 while (!alarm_rang) {
370 printf("Time moved backwards, rescheduled for %s\n", isotime(tm));
374 if (now - t <= slack) // still about time
376 else { // reschedule to next event
378 printf("Missed execution at %s\n", isobuf);
379 t = find_next(now + 1);
382 printf("Snoozing until %s\n", isotime(tm));
385 // do some sleeping, but not more than SLEEP_PHASE
388 ts.tv_sec = t - now > SLEEP_PHASE ? SLEEP_PHASE : t - now;
391 // we just iterate again when this exits early
397 tm = localtime(&now);
398 printf("Starting execution at %s\n", isotime(tm));
401 // no command to run, the outside script can go on
405 execvp(argv[optind], argv+optind);