From owner-freebsd-questions Mon Feb 12 16: 3:15 2001 Delivered-To: freebsd-questions@freebsd.org Received: from dsl-64-193-218-89.telocity.com (dsl-64-193-218-89.telocity.com [64.193.218.89]) by hub.freebsd.org (Postfix) with SMTP id BFD4337B491 for ; Mon, 12 Feb 2001 16:03:10 -0800 (PST) Received: (qmail 6782 invoked by uid 1000); 13 Feb 2001 00:00:15 -0000 Date: Mon, 12 Feb 2001 18:00:15 -0600 From: Lucas Bergman To: Doug Young Cc: freebsd-questions@freebsd.org Subject: Re: Scripting issue - pppd Message-ID: <20010212180015.A16376@billygoat.slb.to> Reply-To: lucas@slb.to References: <02a601c09432$54a5c6c0$847e03cb@apana.org.au> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5i In-Reply-To: <02a601c09432$54a5c6c0$847e03cb@apana.org.au>; from dougy@bryden.apana.org.au on Sun, Feb 11, 2001 at 11:55:40PM +1000 Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG > I need to setup a script to disconnect dialin (pppd) users after > they have been online for more than 8 hours in any one 24 hour > period but haven't a clue where to start. Finding out how long a process has been running from the shell is the hard part of this task. The following C program will tell you how long (in seconds) a process has been running. For example, if I do % gcc -Wall -O -o howlong howlong.c % ./howlong 1 2592578 I get that `init' has been running 2.59 million seconds, or just a smidge over 30 days. This makes sense, since % uptime 5:57PM up 30 days, 6 mins, 1 user, load averages: 0.07, 0.02, 0.00 Note that this program is FreeBSD-specific, since it uses /proc in an essential way. The rest of your problem should be easy, if not trivial, shell scripting; good luck. Lucas -------------- begin howlong.c -------------------- #include #include #include #include #include void usage(); int main(int argc, char **av) { unsigned long pid, start; char *end; char procdir[80]; /* never overflows, if av[1] is valid */ char starttime[20]; /* never overflows */ FILE *f; int c, field, i = 0; if (argc != 2) { usage(); exit(1); } pid = strtoul(av[1], &end, 10); if (*end) { usage(); exit(1); } strcpy(procdir, "/proc/"); strcat(procdir, av[1]); if (chdir(procdir) == -1) { fprintf(stderr, "howlong: fatal: could not change to %s\n", procdir); fprintf(stderr, "Probably, the process ID %s is not running.\n", av[1]); exit(2); } f = fopen("status", "r"); if (!f) { fprintf(stderr, "howlong: panic: could not open %s/status\n", procdir); exit(3); } field = 1; while (field < 8 && (c = fgetc(f)) >= 0) if ((char) c == ' ') ++field; if (c == EOF) { fclose(f); fprintf(stderr, "howlong: panic: could not get time field from status\n"); exit(4); } while ((c = fgetc(f)) >= 0 && ((char) c != ',')) { if (i > 18) { /* we would overflow starttime */ fclose(f); fprintf(stderr, "howlong: panic: bad start time in status\n"); exit(5); } starttime[i++] = (char) c; } fclose(f); starttime[i] = 0; start = strtoul(starttime, &end, 10); if (*end) { fprintf(stderr, "howlong: panic: bad start time in status\n"); exit(6); } printf("%ld\n", time(0) - start); return 0; } void usage() { fprintf(stderr, "usage: howlong pid\n"); } -------------- end howlong.c ---------------------- To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message