2 * snooze - run a command at a particular time
4 * To the extent possible under law,
5 * Christian Neukirchen <chneukirchen@gmail.com>
6 * has waived all copyright and related or neighboring rights to this work.
7 * http://creativecommons.org/publicdomain/zero/1.0/
11 ##% gcc -Os -Wall -g -o $STEM $FILE -Wextra -Wwrite-strings
15 #include <sys/types.h>
30 static long slack = 60;
31 #define SLEEP_PHASE 300
32 static int nflag, vflag;
34 static int timewait = -1;
35 static int randdelay = 0;
36 static char *timefile;
38 static sig_atomic_t alarm_rang = 0;
48 parse_int(char **s, size_t minn, size_t maxn)
54 n = strtol(*s, &end, 10);
59 if (n < (long)minn || n >= (long)maxn) {
60 fprintf(stderr, "number outside %zd <= n < %zd\n", minn, maxn);
74 n = strtol(s, &end, 10);
80 fprintf(stderr, "negative duration\n");
84 case 'm': n *= 60; break;
85 case 'h': n *= 60*60; break;
86 case 'd': n *= 24*60*60; break;
89 fprintf(stderr, "junk after duration: %s\n", end);
96 parse(char *expr, char *buf, long bufsiz, int offset)
99 long i, n = 0, n0 = 0;
101 memset(buf, ' ', bufsiz);
106 case '0': case '1': case '2': case '3': case '4':
107 case '5': case '6': case '7': case '8': case '9':
108 n = parse_int(&s, -offset, bufsiz);
114 n = parse_int(&s, -offset, bufsiz);
115 for (i = n0; i <= n; i++)
121 n = parse_int(&s, -offset, bufsiz);
124 for (i = n0; i < bufsiz; i += n)
134 for (i = 0; i < bufsiz; i++)
138 fprintf(stderr, "can't parse: %s %s\n", expr, s);
146 char weekday[8] = {0};
147 char dayofmonth[31] = {0};
148 char month[12] = {0};
149 char dayofyear[366] = {0};
150 char weekofyear[54] = {0};
152 char minute[60] = {0};
153 char second[61] = {0};
156 isoweek(struct tm *tm)
158 /* ugh, but easier than the correct formula... */
161 strftime(weekstr, sizeof weekstr, "%V", tm);
162 return parse_int(&w, 1, 54);
166 find_next(time_t from)
175 while (!(weekday[tm->tm_wday] == '*'
176 && dayofmonth[tm->tm_mday-1] == '*'
177 && month[tm->tm_mon] == '*'
178 && weekofyear[isoweek(tm)] == '*'
179 && dayofyear[tm->tm_yday] == '*')) {
180 if (month[tm->tm_mon] != '*') {
181 // if month is not good, step month
193 if (t > from+(365*24*60*60)) // no result within a year
197 int y = tm->tm_yday; // save yday
199 while (!(hour[tm->tm_hour] == '*'
200 && minute[tm->tm_min] == '*'
201 && second[tm->tm_sec] == '*')) {
202 if (hour[tm->tm_hour] != '*') {
206 } else if (minute[tm->tm_min] != '*') {
213 if (tm->tm_yday != y) // hit a different day, retry...
220 static char isobuf[25];
222 isotime(const struct tm *tm)
224 strftime(isobuf, sizeof isobuf, "%FT%T%z", tm);
228 int main(int argc, char *argv[])
232 time_t now = time(0);
235 /* default: every day at 00:00:00 */
236 memset(weekday, '*', sizeof weekday);
237 memset(dayofmonth, '*', sizeof dayofmonth);
238 memset(month, '*', sizeof month);
239 memset(dayofyear, '*', sizeof dayofyear);
240 memset(weekofyear, '*', sizeof weekofyear);
245 while ((c = getopt(argc, argv, "D:W:H:M:S:T:R:d:m:ns:t:vw:")) != -1)
247 case 'D': parse(optarg, dayofyear, sizeof dayofyear, -1); break;
248 case 'W': parse(optarg, weekofyear, sizeof weekofyear, -1); break;
249 case 'H': parse(optarg, hour, sizeof hour, 0); break;
250 case 'M': parse(optarg, minute, sizeof minute, 0); break;
251 case 'S': parse(optarg, second, sizeof second, 0); break;
252 case 'd': parse(optarg, dayofmonth, sizeof dayofmonth, -1); break;
253 case 'm': parse(optarg, month, sizeof month, -1); break;
254 case 'w': parse(optarg, weekday, sizeof weekday, 0);
255 // special case: sunday is both 0 and 7.
256 if (weekday[7] == '*')
259 case 'n': nflag++; break;
260 case 'v': vflag++; break;
261 case 's': slack = parse_dur(optarg); break;
262 case 'T': timewait = parse_dur(optarg); break;
263 case 't': timefile = optarg; break;
264 case 'R': randdelay = parse_dur(optarg); break;
266 fprintf(stderr, "Usage: %s [-nv] [-t timefile] [-T timewait] [-R randdelay] [-s slack]\n"
267 " [-d mday] [-m mon] [-w wday] [-D yday] [-W yweek] [-H hour] [-M min] [-S sec] COMMAND...\n"
268 "Timespec: exact: 1,3,5\n"
270 " every n-th: /10\n", argv[0]);
274 time_t start = now + 1;
278 if (stat(timefile, &st) < 0) {
281 t = start - slack - 1 - timewait;
285 if (timewait == -1) {
286 while (t < start - slack)
287 t = find_next(t + 1);
290 if (t + timewait > start)
291 start = st.st_mtime + timewait;
298 long rnd = getauxval(AT_RANDOM);
300 delay = rnd % randdelay;
304 srand48(getpid() ^ start);
305 delay = lrand48() % randdelay;
308 printf("randomly delaying by %lds.\n", delay);
312 t = find_next(start);
314 fprintf(stderr, "no satisfying date found within a year.\n");
319 /* dry-run, just output the next 5 dates. */
321 for (i = 0; i < 5; i++) {
324 struct tm *tm = localtime(&t);
325 strftime(weekstr, sizeof weekstr, "%a", tm);
326 printf("%s %s %2ldd%3ldh%3ldm%3lds\n",
329 ((t - now) / (60*60*24)),
330 ((t - now) / (60*60)) % 24,
331 ((t - now) / 60) % 60,
334 t = find_next(t + 1);
339 struct tm *tm = localtime(&t);
341 printf("Snoozing until %s\n", isotime(tm));
343 // setup SIGALRM handler to force early execution
345 sa.sa_handler = &wakeup;
346 sa.sa_flags = SA_RESTART;
347 sigfillset(&sa.sa_mask);
348 sigaction(SIGALRM, &sa, NULL); // XXX error handling
350 while (!alarm_rang) {
355 printf("Time moved backwards, rescheduled for %s\n", isotime(tm));
359 if (now - t <= slack) // still about time
361 else { // reschedule to next event
363 printf("Missed execution at %s\n", isobuf);
364 t = find_next(now + 1);
367 printf("Snoozing until %s\n", isotime(tm));
370 // do some sleeping, but not more than SLEEP_PHASE
373 ts.tv_sec = t - now > SLEEP_PHASE ? SLEEP_PHASE : t - now;
376 // we just iterate again when this exits early
380 // no command to run, the outside script can go on
386 tm = localtime(&now);
387 printf("Starting execution at %s\n", isotime(tm));
390 execvp(argv[optind], argv+optind);