From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,2635b258e2a1fc0f X-Google-Attributes: gidfee84,public From: robm@test-kiosk.eecs.berkeley.edu (Rob McNicholas) Subject: trimming wtmp (was: /usr filesystem overloaded) Date: 1996/08/23 Message-ID: #1/1 X-Deja-AN: 176067346 sender: robm@test-kiosk.eecs.berkeley.edu references: organization: Technical and Computing Services, U.C. Berkeley, Berkeley, CA newsgroups: comp.unix.osf.osf1 Someone asked if there was a better way to maintain the wtmp file instead of just truncating it. Attached is a script i wrote many years ago which prunes records from the top of the file, leaving the most recent records; i have been using it on our Ultrix 4.2 systems ever since. Don't know what will happen under DU. Use at your own risk. -rob #!/bin/csh -f # usage: trimwtmp [numrec] # script to trim wtmp # Rob McNicholas, 1/30/91 # version of 3/18/92 # # Deletes the first _numrec_ entries from the /usr/adm/wtmp file. # If _numrec_ is not specified, 1000 is the default. # # Suggested use is as a crontab entry, e.g. # 42 4 15,30 * * sh /usr/etc/trimwtmp 2>&1 | Mail -s "wtmp trim" root # # Bugs: # Doesn't check to be sure argument is an integer # # the default number of records to trim set numrec_default = 1000 # size of one wtmp record set recsize = 36 # file to act on set wtmpfile = /usr/adm/wtmp # current size of file set filesize = `wc -c $wtmpfile` # trim off file name from wc's output set filesize = $filesize[1] # number of records to delete if ( $#argv == 0 ) then # user didn't specify how many records, so use default set numrec_del = $numrec_default else if ($#argv == 1) then # use number from command line (and hope it's a number!) set numrec_del = $1 else echo "Usage: $0 [numrec]" exit (1) endif echo "Trimming $numrec_del records" # current number of records in the file @ cur_rec = `expr $filesize / $recsize` # number of records to keep is cur_rec - numrec_del @ keep = `expr $cur_rec - $numrec_del` if ($keep < 0) then echo -n "Tried to delete more records ($numrec_del) than actually " echo "exist ($cur_rec)" exit (1) else # put what we are deleting in a 'old' file dd if=$wtmpfile of=/usr/adm/wtmp.old bs=$recsize count=$numrec_del # put what we want to keep in a temporary file dd if=$wtmpfile of=/usr/adm/wtmp.$$ bs=$recsize skip=$numrec_del # trash the original file and replace with our shortened version mv -f /usr/adm/wtmp.$$ $wtmpfile # compress the 'old' data (-f forces overwrite if file already exists) compress -f /usr/adm/wtmp.old # say what happened echo "Successfully trimmed $numrec_del records from $wtmpfile" echo "Old records are in /usr/adm/wtmp.old.Z" exit(0) endif # EOF: trimwtmp