Snippets

Iakov Davydov notify_old

Created by Iakov Davydov last modified
1
2
3
4
5
6
7
8
9
## 1. this is contents of your crontab file. run crontab -e to add these lines.
## 2. you can use your username or your email as the last argument.
## 3. make sure you are installing this on the machine where the directory is accessible,
##    i.e. if you check /scratch/local/... edit crontab on the machine with the directory of interest.
## 4. use the absolute path to you directory, not the relative.
## 5. this will check for old files (20 days and older) every day at noon (12:00); as a suggestion replace 
##    this with something else not to overload the server at the same time.
##    e.g. 27 15 * * * will run the script at 15:27.
0 12 * * * /home/USERNAME/bin/notify_old.sh /scratch/beegfs/monthly/USERNAME/ 20 USERNAME
#!/bin/bash
# this file needs to be executable, use chmod +x notify_old.sh
# idea: Kamil Jaron, implementation: Iakov Davydov
FILES_PATH="$1"
DAYS=$2
EMAIL="$3"

if [[ ! $EMAIL ]]
then
    	echo "Usage: $(basename $0) DIR NDAYS EMAIL"
        exit 1
fi

cd "$FILES_PATH" || { echo "Couldn't change path, exiting " 1>&2 ; exit 1; }
N_OLD_FILES=$(find -type f -mtime +$DAYS | wc -l)
OLD_FILES=$(find -type f -mtime +$DAYS 2> /dev/null | head -n 10)

if [[ $OLD_FILES ]]
then
    	mail -s "Files will be deleted soon" $EMAIL << END
There are $N_OLD_FILES files which will be deleted soon in $FILES_PATH.
These files are older than $DAYS days.
Here are some files:
$OLD_FILES
END

fi

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.