How to find expired user accounts in Linux

Q. I have a requirement to check whose accounts are expired in Linux machine and send a mail to root user about the accounts. How can i achieve this in Linux?

Ans : Here is the script to check the account expires of the users in Linux

#Created on:09-02-2010
#Last modified:02-05-2010
#Purpose:To check the user account status in Linux
#Author:Surendra Kumar Anne
#The below command will grep all the users in shadow file
#whose expiry time is set and send them to expirelist.txt
cat /etc/shadow | cut -d: -f1,8 | sed /:$/d > /tmp/expirelist.txt
totalaccounts=`cat /tmp/expirelist.txt | wc -l`
for((i=1; i<=$totalaccounts; i++ ))
tuserval=`head -n $i /tmp/expirelist.txt | tail -n 1`
username=`echo $tuserval | cut -f1 -d:`
userexp=`echo $tuserval | cut -f2 -d:`
userexpireinseconds=$(( $userexp * 86400 ))
todaystime=`date +%s`
#check if the user expired or not?
if [ $userexpireinseconds -ge $todaystime ] ;
timeto7days=$(( $todaystime + 604800 ))
if [ $userexpireinseconds -le $timeto7days ];
mail -s "The account $username will expire less than 7 days" root
mail -s "The user account $username already expired" root

This script will send a mail to root about the status of expired and going to expire user accounts.

Show your love by sharing this..!
Share on VKShare on StumbleUponShare on YummlyPrint this pageFlattr the authorShare on TumblrEmail this to someoneBuffer this pageShare on Google+Share on LinkedInShare on FacebookTweet about this on TwitterPin on PinterestShare on RedditDigg this
The following two tabs change content below.
My name is Surendra Kumar Anne. I hail from Vijayawada which is capital of south Indian state of Andhra Pradesh. I am a Linux evangelist who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. I work with ICE systems, Sydney as Sr. Consultant. You can contact me at surendra (@) linuxnix dot com.
  • Pingback: Linux Shadow file explained in detail | The Linux Juggernaut()

  • meow

    This would be much shorter if using awk:
    awk ‘BEGIN{FS=”:”;printf(“%-24s%sn”,”USER”,”DAYS_LEFT”)}$8!~/^$/{printf(“%-24s%dn”, $1,($8*86400-strftime(“%s”))/86400)}’ /etc/shadow

    • Surendra Anne

      Thanks for sharing your knowledge Meow. Time gives you the experience. Keep visiting