Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Delete Expired Accounts

    Hi, I have created a vsftpd server on CentOS 6.2 and have a script which allows the creation of FTP users within an expiration date.

    useradd -s /sbin/nologin -c 'Full Name' -e 2012-07-04 user_name
    passwd user_name
    I need to be able to write a script which will search for all expired users and delete them plus their home directories. This script will then need to be run via cron each night.

    Can anyone help me with this as struggling a bit

    Last edited by pshankland; 07-09-2012 at 12:02 PM.

  2. #2
    The chage utility can help you here. use it to get the account expiration date, then use the GNU date command to calculate time since the epoch.

    This is a sample script that works for my simple tests - you can tweak it to suit your needs, maybe.

    # current time in seconds
    datenow=$(date +%s)
    # loop over all users
    for user in $(awk -F: '{print $1}' /etc/passwd); do
      # get date of expiration of users that have exp. date set
      expdate=$(chage -l $user|awk -F: '/Account expires/{print $2}')
      echo $expdate|grep -q never && continue
      echo -n "User \`$user' expires on $expdate ... "
      # convert exp. date to seconds
      expsec=$(date +%s --date="$expdate")
      # calculate diff b/t time now and exp. date
      diff=$(echo $datenow - $expsec|bc -l)
      # if a negative number, hasn't expired yet
      echo $diff|grep -q ^\- && echo okay && continue
      # got this far, account must have expired
      echo deleting...
    #  userdel -r $user
    Note: i've commented out the actual command to delete the users (userdel). make sure it is working properly before uncommenting that!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts