Difference between revisions of "Test file age"
From Noah.org
Jump to navigationJump to search (New page: Category:Engineering This shows how to check if a file is older than a given date using Bash. I use this to test the age of my server backup log. If the log is more than a day old then...) |
|||
(One intermediate revision by the same user not shown) | |||
Line 3: | Line 3: | ||
<pre> | <pre> | ||
− | MAXAGE= | + | #!/bin/sh |
+ | # This checks that the specified file is less than 24 hours old. | ||
+ | # returns 0 if younger than 24 hours. | ||
+ | # returns 1 if older than 24 hours. | ||
+ | FILE=/home/backup/BACKUP_TIMESTAMP | ||
+ | MAXAGE=$(bc <<< '24*60*60') # seconds in a day | ||
# file age in seconds = current_time - file_modification_time. | # file age in seconds = current_time - file_modification_time. | ||
FILEAGE=$(($(date +%s) - $(stat -c '%Y' "$FILE"))) | FILEAGE=$(($(date +%s) - $(stat -c '%Y' "$FILE"))) | ||
test $FILEAGE -lt $MAXAGE && { | test $FILEAGE -lt $MAXAGE && { | ||
echo "$FILE is less than $MAXAGE seconds old." | echo "$FILE is less than $MAXAGE seconds old." | ||
+ | exit 0 | ||
} | } | ||
− | + | echo "$FILE is older than $MAXAGE seconds." | |
+ | exit 1 |
Latest revision as of 19:39, 6 December 2007
This shows how to check if a file is older than a given date using Bash. I use this to test the age of my server backup log. If the log is more than a day old then I send an alert.
#!/bin/sh # This checks that the specified file is less than 24 hours old. # returns 0 if younger than 24 hours. # returns 1 if older than 24 hours. FILE=/home/backup/BACKUP_TIMESTAMP MAXAGE=$(bc <<< '24*60*60') # seconds in a day # file age in seconds = current_time - file_modification_time. FILEAGE=$(($(date +%s) - $(stat -c '%Y' "$FILE"))) test $FILEAGE -lt $MAXAGE && { echo "$FILE is less than $MAXAGE seconds old." exit 0 } echo "$FILE is older than $MAXAGE seconds." exit 1