2

I want to create account expiry alerts therefore I need to write a script which could alert me before 1 month of expiry over email. I'm working on Linux.

I can get expiry information as 2015-27-03. I want if 31 days are left in 2015-27-03 by determining current date then I could get an email alert. I'm unable to write a correct if statement.

  • In what way do you want to be 'alerted'? If you want a desktop notification, you could use notify-send. If you want an email, you could use mail. – Graeme Mar 27 '14 at 13:03
  • Following your edit, I have update my answer – Laurent C. Mar 28 '14 at 11:27
2

I'm not really sure where you are stuck because you did not provide a lot of information or example, but you could consider the following commands:

  • chage -l userName to check the expiration date of a user's password
  • mail to send an email either to the user or to the admin (or both) (as said by Graeme in his comment)

With these two commands, you should be able to write a simple script to check for password expiration.

You could also use crontab for the scheduling (daily basis for instance).

EDIT:

Following your edit with more information, you can try as follows:

# Get the current date in seconds since 1970
current_date=`date "+%s"`
# Convert the date you want to check in seconds since 1970
date_to_check=`date -d 2015/03/31 +"%s"`
# Calculate the difference in seconds
date_diff=`expr $date_to_check - $current_date`
# Check whether the difference is greater than 31 days (2678400 seconds)
if [ `expr $date_diff - 2678400` -gt 0 ]
then
    echo More than 31 days left
else
    echo Less than 31 days left
fi
  • If i want equal to 31 days then may i use if [ expr $date_diff - 2678400 -eq 0 ] then echo 31 days left else echo Less or More than 31 days left fi – User4283 Mar 31 '14 at 8:58
  • Yes and then you will have to run the script everyday (via crontab for instance) to make sure that you don't miss the exact date of 31 days before expiration. – Laurent C. Mar 31 '14 at 9:06
  • May i use any date format in date_to_check either 2014/02/28 , 28/02/2014 , 28-02-2014, 28-feb-2014? – User4283 Mar 31 '14 at 10:16
  • It may depend on your system. I tested the script on a Debian based OS in English. You can do the test date -d 2015/03/31 with the format you would like to use and check whether your OS allows this syntax. – Laurent C. Mar 31 '14 at 10:29
  • If date is any of above format i want it convert in seconds. Is there any solution with perl or bash? – User4283 Mar 31 '14 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.