Archiving Users

Hello Gurus,
I have a requirement where I have to remove users that have not logged into our unix box last 90 days. I want to script this one and remove the users automatically now and then. Has any one ever done this before, if so please give me some sample code. I am not a good unix programmer and I would appreciate any help on this issue.

Thanks,
Sathish Kumar

Hi Sathish I'm listing below a script that should peform what u have desired.

#! /bin/bash
#lastlog|awk -F " " '{print $1" "$3" "$4" " $5" " $6" " $7" " $8}'|grep [1-31] > lastlogfile
lastlog|tr -s " " |cut -f 1,3-8 -d" "|grep [1-31] >lastlogfile
terminal=`tty`
backday=`date --date='90 days ago'`
backdaysec=`date --date="$backday" +%s`

exec<lastlogfile

while read line
do
#	lastdate=`echo $line| awk -F " " '{print $2" "$3" "$4" "$5" "$6" "$7}'`
	lastdate=`echo $line| cut -f 3-8 -d" "`
	lastdatesec=`date --date="$lastdate" +%s`
	if [ $lastdatesec -lt $backdaysec ]
	then
#		echo $line | awk -F " " '{print $1}'
		user=`echo $line | cut -f 1 -d" " | tr -d "\n"`
                         userdel -r $user
	fi
done
exec<$terminal
#rm -fr lastlogfile

This code whenever run will delete the users who haven't logged in for more than 90 days. Now u can either submit this script to be run as a cron job at the end of the day or put put the whole script in a infinte loop
(like
while 1
do
#put above script here
done
)
and run the above script in background (like nohup ./rmuser &). It is advisable to use crontab for this would not put unnecessary pressure on ur system. There is much of documentation on crontab (use info & man) in addition to these u can look at http://www.deluxnetwork.com/linux/guides/crons.php for help.

In the above script I've deliberately left some part commented. While codes involving awk present an alternative to code involving cut following it. The last command rm -fr is commented so that the file (lastlogfile) need not be created everytime but may be uncommented if that needs to be removed for some reason.
The code is quite easy to comprehend so I'm not giving much of description but in short it obtaines the last login time and corresp. login name of all users using command 'lastlog' which uses file /var/log/lastlog (if it doesn't exist create one) converts the last login time to seconds since epoch (which is 1970-01-01 00:00:00 UTC). In the similar fashion it converts the date 90 days ago (since the time the code is being executed) to seconds since epoch. Now it compares the two times and removes the user if his last login is older than 90 days.

Two caveats :
1) If a user is logged in continuously for 90 days (i.e. he logged in 90 days back and is still logged in) the code might attempt to delete his account (though it might not succeed for userdel might not allow it to do so).

2) I have used userdel -r. The option 'r' should not be used if u want his files to be left (home directory and files therein).

Hope this helps u

Regards
Rakesh Ranjan

Rakaesh,
Thanks for your help. I really appreciate it. I will update the status soon.

Regards,
Sathish Kumar