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[])
204 time_t now = time(0);
206 /* default: every day at 00:00:00 */
207 memset(weekday, '*', sizeof weekday);
208 memset(dayofmonth, '*', sizeof dayofmonth);
209 memset(month, '*', sizeof month);
210 memset(dayofyear, '*', sizeof dayofyear);
211 memset(weekofyear, '*', sizeof weekofyear);
216 while ((c = getopt(argc, argv, "D:W:H:M:S:T:R:d:m:ns:t:vw:")) != -1)
218 case 'D': parse(optarg, dayofyear, sizeof dayofyear, -1); break;
219 case 'W': parse(optarg, weekofyear, sizeof weekofyear, -1); break;
220 case 'H': parse(optarg, hour, sizeof hour, 0); break;
221 case 'M': parse(optarg, minute, sizeof minute, 0); break;
222 case 'S': parse(optarg, second, sizeof second, 0); break;
223 case 'd': parse(optarg, dayofmonth, sizeof dayofmonth, -1); break;
224 case 'm': parse(optarg, month, sizeof month, -1); break;
225 case 'w': parse(optarg, weekday, sizeof weekday, 0);
226 // special case: sunday is both 0 and 7.
227 if (weekday[7] == '*')
230 case 'n': nflag++; break;
231 case 'v': vflag++; break;
232 case 's': slack = atoi(optarg); break;
233 case 'T': timewait = atoi(optarg); break;
234 case 't': timefile = optarg; break;
235 case 'R': randdelay = atoi(optarg); break;
237 fprintf(stderr, "Usage: %s [-nv] [-t timefile] [-T timewait] [-R randdelay] [-s slack]\n"
238 " [-d mday] [-m mon] [-w wday] [-D yday] [-W yweek] [-H hour] [-M min] [-S sec] COMMAND...\n"
239 "Timespec: exact: 1,3,5\n"
241 " every n-th: /10\n", argv[0]);
245 time_t start = now + 1;
249 if (stat(timefile, &st) < 0) {
252 t = start - slack - 1 - timewait;
256 if (timewait == -1) {
257 while (t < start - slack)
258 t = find_next(t + 1);
261 if (t + timewait > start)
262 start = st.st_mtime + timewait;
269 long rnd = getauxval(AT_RANDOM);
271 delay = rnd % randdelay;
275 srand48(getpid() ^ start);
276 delay = lrand48() % randdelay;
279 printf("randomly delaying by %lds.\n", delay);
283 t = find_next(start);
285 fprintf(stderr, "no satisfying date found within a year.\n");
290 /* dry-run, just output the next 5 dates. */
292 for (i = 0; i < 5; i++) {
294 printf("%s\n", isotime(localtime(&t)));
295 t = find_next(t + 1);
300 struct tm *tm = localtime(&t);
302 printf("Snoozing until %s\n", isotime(tm));
304 // setup SIGALRM handler to force early execution
306 sa.sa_handler = &wakeup;
307 sa.sa_flags = SA_RESTART;
308 sigfillset(&sa.sa_mask);
309 sigaction(SIGALRM, &sa, NULL); // XXX error handling
311 while (!alarm_rang) {
315 if (now - t <= slack) // still about time
317 else { // reschedule to next event
319 printf("Missed execution at %s\n", isobuf);
320 t = find_next(now + 1);
323 printf("Snoozing until %s\n", isotime(tm));
326 // do some sleeping, but not more than SLEEP_PHASE
329 ts.tv_sec = t - now > SLEEP_PHASE ? SLEEP_PHASE : t - now;
331 // we just iterate again when this exits early
335 // no command to run, the outside script can go on
341 tm = localtime(&now);
342 printf("Starting execution at %s\n", isotime(tm));
345 execvp(argv[optind], argv+optind);