Expiration Warning Script

Some free services on the internet want you to login to their web site every now and then so they know that you are still actively using their service. One of those services is www.vps.me. They offer a free Virtual Private Server, which is very nice of course. However if you don't login to your account once every 30 days they will simply destroy your precious VPS, without warning.
It's not only the free services which can cause problems. For my work I have 3 accounts on a web portal of one of our suppliers. Each account has a different role, one of which is the security manager. If you don't login to your account at least once a month your password will expire and you will have to ask your security manager to reset your password. Uhm, but I AM the security manager. As security manager I rarely have to login. But this policy forces me to login once a month, otherwise I will have to contact our account manager in order to get the ff-ing account reactivated.

So it would be nice to get a warning, in time, to login to these accounts to avoid account expiration. And that is just what this little script will do for you.

The Script

Copy the Bash script below into a file and make it executable. After copying the file you will have to alter at least the email address to which the warnings must be sent. You can also alter the SUBJECT and MESSAGE variables to give you a clearer indication of what the warning is all about.
The EXPIRE_DATE variable does not have to be changed. It will be set automatically when you run the script later on.
The default interval is set to 21 days in this example. You can make it any number of days you like.

Once you have set up all these variables it is time to run the script. When you run the script from within the terminal it will set the expiration date to 21 days from now (or any other value you have set).
You can also set another interval by supplying an integer as parameter to the script.

Now all you have to do is to setup a crontab line which calls this script once a day. You will receive an email from the script, informing you that it is time to login again. If you don't respond to it you will continue to get a warning every day, until you reset the expiration date.
Resetting the expiration date is as simple as running the script manually again. That will advance the next warning by 21 days (per default).

Needless to say is that the machine which runs this script should be on at the time the script is run by cron and that it can send emails.

#! /bin/bash

# This script will warn you when it is time to log in to
# any particular web site again to avoid expiration of
# the password or entire account.

# If it is run by crontab it will send you an e-mail,
# notifying you when it's time to log in again..
# If it is run from the shell it will set the next
# date on which to warn you.
# If you run it with an integer argument it will set
# the next warning date to today+n

# Person to warn
        EMAIL="[email protected]"
# Subject line of email
        SUBJECT="It is about time to log-in again."
# Message text of email
        MESSAGE="Otherwise the account will expire."
        MESSAGE+="\n\nYou can set the new reminder by running $0 manually."
# Next date on which a warning is issued (automatically updated)
        EXPIRE_DATE="2014-09-19"
# Default number of days until next warning
        INTERVAL=21

TODAY=$(date +%Y-%m-%d)

if [ -t 0 ]
then
        # Running interactively

        # Override default timeout if parameter is given
        if [ $# -eq 1 ]
        then
                INTERVAL=$1
        fi

        # Change the expiration date in this script
        NEXT_DATE=$(date +%Y-%m-%d -d "+$INTERVAL days")
        sed -i "s/EXPIRE_DATE=\"$EXPIRE_DATE\"/EXPIRE_DATE=\"$NEXT_DATE\"/g" "$0"

        # Make the date human readable and print message
        NEW_DATE=$(date +%d-%m-%Y -d "$NEXT_DATE")
        echo
        echo "The next reminder e-mail is set to $NEW_DATE, which is in $INTERVAL days from today."
        echo "Don't forget to actually login today, if you haven't done so already."
        echo
else
        # Running from crontab

        # Convert dates to seconds since epoch
        NOW=$(date +%s -d "$TODAY")
        NEXT=$(date +%s -d "$EXPIRE_DATE")

        # See if past threshold
        if [ "$NOW" -ge "$NEXT" ]
        then
                echo -e "$MESSAGE" | mail -s "$SUBJECT" "$EMAIL"
        fi
fi

A final remark. If you need multiple warnings, which don't necessarily have to stay in sync all year, you can make several copies of this script. Each copy will handle one particular expiration warning.