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[31] = {0};
144 char month[12] = {0};
145 char dayofyear[366] = {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)
171 while (!(weekday[tm->tm_wday] == '*'
172 && dayofmonth[tm->tm_mday-1] == '*'
173 && month[tm->tm_mon] == '*'
174 && weekofyear[isoweek(tm)] == '*'
175 && dayofyear[tm->tm_yday] == '*')) {
176 if (month[tm->tm_mon] != '*') {
177 // if month is not good, step month
189 if (t > from+(366*24*60*60)) // no result within a year
193 int y = tm->tm_yday; // save yday
195 while (!(hour[tm->tm_hour] == '*'
196 && minute[tm->tm_min] == '*'
197 && second[tm->tm_sec] == '*')) {
198 if (hour[tm->tm_hour] != '*') {
202 } else if (minute[tm->tm_min] != '*') {
209 if (tm->tm_yday != y) // hit a different day, retry...
216 static char isobuf[25];
218 isotime(const struct tm *tm)
220 strftime(isobuf, sizeof isobuf, "%FT%T%z", tm);
224 int main(int argc, char *argv[])
228 time_t now = time(0);
231 /* default: every day at 00:00:00 */
232 memset(weekday, '*', sizeof weekday);
233 memset(dayofmonth, '*', sizeof dayofmonth);
234 memset(month, '*', sizeof month);
235 memset(dayofyear, '*', sizeof dayofyear);
236 memset(weekofyear, '*', sizeof weekofyear);
241 while ((c = getopt(argc, argv, "+D:W:H:M:S:T:R:d:m:ns:t:vw:")) != -1)
243 case 'D': parse(optarg, dayofyear, sizeof dayofyear, -1); break;
244 case 'W': parse(optarg, weekofyear, sizeof weekofyear, -1); break;
245 case 'H': parse(optarg, hour, sizeof hour, 0); break;
246 case 'M': parse(optarg, minute, sizeof minute, 0); break;
247 case 'S': parse(optarg, second, sizeof second, 0); break;
248 case 'd': parse(optarg, dayofmonth, sizeof dayofmonth, -1); break;
249 case 'm': parse(optarg, month, sizeof month, -1); break;
250 case 'w': parse(optarg, weekday, sizeof weekday, 0);
251 // special case: sunday is both 0 and 7.
252 if (weekday[7] == '*')
255 case 'n': nflag++; break;
256 case 'v': vflag++; break;
257 case 's': slack = parse_dur(optarg); break;
258 case 'T': timewait = parse_dur(optarg); break;
259 case 't': timefile = optarg; break;
260 case 'R': randdelay = parse_dur(optarg); break;
262 fprintf(stderr, "Usage: %s [-nv] [-t timefile] [-T timewait] [-R randdelay] [-s slack]\n"
263 " [-d mday] [-m mon] [-w wday] [-D yday] [-W yweek] [-H hour] [-M min] [-S sec] COMMAND...\n"
264 "Timespec: exact: 1,3,5\n"
266 " every n-th: /10\n", argv[0]);
270 time_t start = now + 1;
274 if (stat(timefile, &st) < 0) {
277 t = start - slack - 1 - timewait;
281 if (timewait == -1) {
282 while (t < start - slack)
283 t = find_next(t + 1);
286 if (t + timewait > start)
287 start = st.st_mtime + timewait;
294 long rnd = getauxval(AT_RANDOM);
296 delay = rnd % randdelay;
300 srand48(getpid() ^ start);
301 delay = lrand48() % randdelay;
304 printf("randomly delaying by %lds.\n", delay);
308 t = find_next(start);
310 fprintf(stderr, "no satisfying date found within a year.\n");
315 /* dry-run, just output the next 5 dates. */
317 for (i = 0; i < 5; i++) {
319 struct tm *tm = localtime(&t);
320 strftime(weekstr, sizeof weekstr, "%a", tm);
321 printf("%s %s %2ldd%3ldh%3ldm%3lds\n",
324 ((t - now) / (60*60*24)),
325 ((t - now) / (60*60)) % 24,
326 ((t - now) / 60) % 60,
328 t = find_next(t + 1);
331 "no satisfying date found within a year.\n");
338 struct tm *tm = localtime(&t);
340 printf("Snoozing until %s\n", isotime(tm));
342 // setup SIGALRM handler to force early execution
344 sa.sa_handler = &wakeup;
345 sa.sa_flags = SA_RESTART;
346 sigfillset(&sa.sa_mask);
347 sigaction(SIGALRM, &sa, NULL); // XXX error handling
349 while (!alarm_rang) {
354 printf("Time moved backwards, rescheduled for %s\n", isotime(tm));
358 if (now - t <= slack) // still about time
360 else { // reschedule to next event
362 printf("Missed execution at %s\n", isobuf);
363 t = find_next(now + 1);
366 printf("Snoozing until %s\n", isotime(tm));
369 // do some sleeping, but not more than SLEEP_PHASE
372 ts.tv_sec = t - now > SLEEP_PHASE ? SLEEP_PHASE : t - now;
375 // we just iterate again when this exits early
379 // no command to run, the outside script can go on
385 tm = localtime(&now);
386 printf("Starting execution at %s\n", isotime(tm));
389 execvp(argv[optind], argv+optind);