Limiting *nix program runtime.
I posted a question to the Neon Guild mailing list a few days ago about a vexing little problem. For Richmond Sunlight I’m taking regular snapshots of the websites of every member of the General Assembly, as an archive for folks to browse months or years from now, to see what sort of promises legislators made, how they presented themselves, etc. That’s done via an automated script, grabbing a couple of sites every day without me interacting with it at all. The trouble is that wget has no function to be limited temporally, so it’ll run forever and ever when it encounters a website with a badly-written CMS that generates recursive links like http://example.com/about/print/print/print/print. The solution is to create a wrapper for wget that puts it on a timer—if it runs longer than a preset limit, it gets cut off. My friend Jeff Uphoff envisioned this solution and then whipped up a quick program in C to accomplish it. I’m sharing it here for googlers looking for a solution to the same problem. Up notes that it’s in the public domain.
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main (int argc, char **argv)
{
if (argc < 3) {
fprintf (stderr, "Usage: %s maxtime command [arguments]\n", *argv);
return 1;
}
alarm (atoi (*++argv)); /* First arg is time limit (in seconds) */
++argv; /* Next arg is command to exec */
/* All remaining args are passed to
executed command as its args */
if (execvp (*argv, argv) != 0) {
perror ("execvp");
return errno;
}
return 0; /* Never reached */
}If you decided to name it alarmlimit.c, just compile it like such:
# gcc -Wall -ansi alarmlimit.c -o alarmlimit
If you wanted to run top for no more than ten seconds, you'd do this:
alarmlimit 10 top
Note that you could also solve this with ulimit -t, but ulimit is based on processing time, and a program that's not especially processor intensive (like wget) could run for much, much longer than the amount of time that you specify.

2 Comments