#!/usr/bin/perl # idlekill # by Jon Jensen # 2003-07-02 use 5.006; # not tested with older versions of Perl use strict; use warnings; no warnings 'void', 'uninitialized'; use Sys::Utmp; use Getopt::Std; sub usage { print <new; my $time = time; my $format = "%-16s%-12s%-10s%-10s"; printf $format . "%s\n", qw( user tty idle inactive action ); while (my $utent = $utmp->getutent) { next unless $utent->user_process; my $user = $utent->ut_user or next; my $uid; if ($user !~ /\D/) { $uid = $user; $user = getpwuid($uid); } else { $uid = getpwnam($user); } next if ! $opt{a} and ( $user eq 'root' or (length($uid) and $uid == 0) ); if (%users) { my $ok = $users{$user}; $ok ||= $users{$uid} if $opt{n}; $ok = ! $ok if $opt{x}; next unless $ok; } my $tty = $utent->ut_line; unless ($tty) { warn "No tty for user $user; skipping\n"; next; } my ($idle, $inactive) = map { $time - $_ } (stat("/dev/$tty"))[8, 9]; $opt{i} and $idle < $opt{i} and next; $opt{I} and $inactive < $opt{I} and next; printf $format, $user, $tty, $idle, $inactive; print("-\n"), next unless $opt{s}; my $ret = system('skill', "-$opt{s}", '-u', $user, '-t', $tty); if ($ret == 0) { print "signaled\n"; } else { print "error\n"; warn "Error returned from skill: $!\n"; } } $utmp->endutent; exit;