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
190 if (t > from+(366*24*60*60)) // no result within a year
194 int y = tm->tm_yday; // save yday
197 hour[tm->tm_hour] == '*' &&
198 minute[tm->tm_min] == '*' &&
199 second[tm->tm_sec] == '*')) {
200 if (hour[tm->tm_hour] != '*') {
204 } else if (minute[tm->tm_min] != '*') {
211 if (tm->tm_yday != y) // hit a different day, retry...
218 static char isobuf[25];
220 isotime(const struct tm *tm)
222 strftime(isobuf, sizeof isobuf, "%FT%T%z", tm);
227 main(int argc, char *argv[])
231 time_t now = time(0);
234 /* default: every day at 00:00:00 */
235 memset(weekday, '*', sizeof weekday);
236 memset(dayofmonth, '*', sizeof dayofmonth);
237 memset(month, '*', sizeof month);
238 memset(dayofyear, '*', sizeof dayofyear);
239 memset(weekofyear, '*', sizeof weekofyear);
244 while ((c = getopt(argc, argv, "+D:W:H:M:S:T:R:d:m:ns:t:vw:")) != -1)
246 case 'D': parse(optarg, dayofyear, sizeof dayofyear, -1); break;
247 case 'W': parse(optarg, weekofyear, sizeof weekofyear, -1); break;
248 case 'H': parse(optarg, hour, sizeof hour, 0); break;
249 case 'M': parse(optarg, minute, sizeof minute, 0); break;
250 case 'S': parse(optarg, second, sizeof second, 0); break;
251 case 'd': parse(optarg, dayofmonth, sizeof dayofmonth, -1); break;
252 case 'm': parse(optarg, month, sizeof month, -1); break;
253 case 'w': parse(optarg, weekday, sizeof weekday, 0);
254 // special case: sunday is both 0 and 7.
255 if (weekday[7] == '*')
258 case 'n': nflag++; break;
259 case 'v': vflag++; break;
260 case 's': slack = parse_dur(optarg); break;
261 case 'T': timewait = parse_dur(optarg); break;
262 case 't': timefile = optarg; break;
263 case 'R': randdelay = parse_dur(optarg); break;
265 fprintf(stderr, "Usage: %s [-nv] [-t timefile] [-T timewait] [-R randdelay] [-s slack]\n"
266 " [-d mday] [-m mon] [-w wday] [-D yday] [-W yweek] [-H hour] [-M min] [-S sec] COMMAND...\n"
267 "Timespec: exact: 1,3,5\n"
269 " every n-th: /10\n", argv[0]);
273 time_t start = now + 1;
277 if (stat(timefile, &st) < 0) {
280 t = start - slack - 1 - timewait;
284 if (timewait == -1) {
285 while (t < start - slack)
286 t = find_next(t + 1);
289 if (t + timewait > start)
290 start = st.st_mtime + timewait;
297 long rnd = getauxval(AT_RANDOM);
299 delay = rnd % randdelay;
303 srand48(getpid() ^ start);
304 delay = lrand48() % randdelay;
307 printf("randomly delaying by %lds.\n", delay);
311 t = find_next(start);
313 fprintf(stderr, "no satisfying date found within a year.\n");
318 /* dry-run, just output the next 5 dates. */
320 for (i = 0; i < 5; i++) {
322 struct tm *tm = localtime(&t);
323 strftime(weekstr, sizeof weekstr, "%a", tm);
324 printf("%s %s %2ldd%3ldh%3ldm%3lds\n",
327 ((t - now) / (60*60*24)),
328 ((t - now) / (60*60)) % 24,
329 ((t - now) / 60) % 60,
331 t = find_next(t + 1);
334 "no satisfying date found within a year.\n");
341 struct tm *tm = localtime(&t);
343 printf("Snoozing until %s\n", isotime(tm));
345 // setup SIGALRM handler to force early execution
347 sa.sa_handler = &wakeup;
348 sa.sa_flags = SA_RESTART;
349 sigfillset(&sa.sa_mask);
350 sigaction(SIGALRM, &sa, NULL); // XXX error handling
352 while (!alarm_rang) {
357 printf("Time moved backwards, rescheduled for %s\n", isotime(tm));
361 if (now - t <= slack) // still about time
363 else { // reschedule to next event
365 printf("Missed execution at %s\n", isobuf);
366 t = find_next(now + 1);
369 printf("Snoozing until %s\n", isotime(tm));
372 // do some sleeping, but not more than SLEEP_PHASE
375 ts.tv_sec = t - now > SLEEP_PHASE ? SLEEP_PHASE : t - now;
378 // we just iterate again when this exits early
382 // no command to run, the outside script can go on
388 tm = localtime(&now);
389 printf("Starting execution at %s\n", isotime(tm));
392 execvp(argv[optind], argv+optind);