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);
68 parse(char *expr, char *buf, long bufsiz, int offset)
71 long i, n = 0, n0 = 0;
73 memset(buf, ' ', bufsiz);
78 case '0': case '1': case '2': case '3': case '4':
79 case '5': case '6': case '7': case '8': case '9':
80 n = parse_int(&s, -offset, bufsiz);
86 n = parse_int(&s, -offset, bufsiz);
87 for (i = n0; i <= n; i++)
93 n = parse_int(&s, -offset, bufsiz);
96 for (i = n0; i < bufsiz; i += n)
106 for (i = 0; i < bufsiz; i++)
110 fprintf(stderr, "can't parse: %s %s\n", expr, s);
118 char weekday[8] = {0};
119 char dayofmonth[31] = {0};
120 char month[12] = {0};
121 char dayofyear[366] = {0};
122 char weekofyear[53] = {0};
124 char minute[60] = {0};
125 char second[61] = {0};
128 isoweek(struct tm *tm)
130 /* ugh, but easier than the correct formula... */
133 strftime(weekstr, sizeof weekstr, "%V", tm);
134 return parse_int(&w, 1, 54);
138 find_next(time_t from)
147 while (!(weekday[tm->tm_wday] == '*'
148 && dayofmonth[tm->tm_mday-1] == '*'
149 && month[tm->tm_mon] == '*'
150 && weekofyear[isoweek(tm)] == '*'
151 && dayofyear[tm->tm_yday] == '*')) {
152 if (month[tm->tm_mon] != '*') {
153 // if month is not good, step month
165 if (t > from+(365*24*60*60)) // no result within a year
169 int y = tm->tm_yday; // save yday
171 while (!(hour[tm->tm_hour] == '*'
172 && minute[tm->tm_min] == '*'
173 && second[tm->tm_sec] == '*')) {
174 if (hour[tm->tm_hour] != '*') {
178 } else if (minute[tm->tm_min] != '*') {
185 if (tm->tm_yday != y) // hit a different day, retry...
192 static char isobuf[25];
194 isotime(const struct tm *tm)
196 strftime(isobuf, sizeof isobuf, "%FT%T%z", tm);
200 int main(int argc, char *argv[])
203 time_t now = time(0);
205 /* default: every day at 00:00:00 */
206 memset(weekday, '*', sizeof weekday);
207 memset(dayofmonth, '*', sizeof dayofmonth);
208 memset(month, '*', sizeof month);
209 memset(dayofyear, '*', sizeof dayofyear);
210 memset(weekofyear, '*', sizeof weekofyear);
215 while ((c = getopt(argc, argv, "D:W:H:M:S:T:R:d:m:ns:t:vw:")) != -1)
217 case 'D': parse(optarg, dayofyear, sizeof dayofyear, -1); break;
218 case 'W': parse(optarg, weekofyear, sizeof weekofyear, -1); break;
219 case 'H': parse(optarg, hour, sizeof hour, 0); break;
220 case 'M': parse(optarg, minute, sizeof minute, 0); break;
221 case 'S': parse(optarg, second, sizeof second, 0); break;
222 case 'd': parse(optarg, dayofmonth, sizeof dayofmonth, -1); break;
223 case 'm': parse(optarg, month, sizeof month, -1); break;
224 case 'w': parse(optarg, weekday, sizeof weekday, 0);
225 // special case: sunday is both 0 and 7.
226 if (weekday[7] == '*')
229 case 'n': nflag++; break;
230 case 'v': vflag++; break;
231 case 's': slack = atoi(optarg); break;
232 case 'T': timewait = atoi(optarg); break;
233 case 't': timefile = optarg; break;
234 case 'R': randdelay = atoi(optarg); break;
236 fprintf(stderr, "Usage: %s [-nv] [-t timefile] [-T timewait] [-R randdelay] [-s slack]\n"
237 " [-d mday] [-m mon] [-w wday] [-D yday] [-W yweek] [-H hour] [-M min] [-S sec] COMMAND...\n"
238 "Timespec: exact: 1,3,5\n"
240 " every n-th: /10\n", argv[0]);
244 time_t start = now + 1;
248 if (stat(timefile, &st) < 0) {
252 if (st.st_mtime + timewait > start)
253 start = st.st_mtime + timewait;
260 long rnd = getauxval(AT_RANDOM);
262 delay = rnd % randdelay;
266 srand48(getpid() ^ start);
267 delay = lrand48() % randdelay;
270 printf("randomly delaying by %lds.\n", delay);
274 time_t t = find_next(start);
276 fprintf(stderr, "no satisfying date found within a year.\n");
281 /* dry-run, just output the next 5 dates. */
283 for (i = 0; i < 5; i++) {
285 printf("%s\n", isotime(localtime(&t)));
286 t = find_next(t + 1);
291 struct tm *tm = localtime(&t);
293 printf("Snoozing until %s\n", isotime(tm));
295 // setup SIGALRM handler to force early execution
297 sa.sa_handler = &wakeup;
298 sa.sa_flags = SA_RESTART;
299 sigfillset(&sa.sa_mask);
300 sigaction(SIGALRM, &sa, NULL); // XXX error handling
302 while (!alarm_rang) {
306 if (now - t <= slack) // still about time
308 else { // reschedule to next event
310 printf("Missed execution at %s\n", isobuf);
311 t = find_next(now + 1);
314 printf("Snoozing until %s\n", isotime(tm));
317 // do some sleeping, but not more than SLEEP_PHASE
320 ts.tv_sec = t - now > SLEEP_PHASE ? SLEEP_PHASE : t - now;
322 // we just iterate again when this exits early
326 // no command to run, the outside script can go on
330 execvp(argv[optind], argv+optind);