jbreg
asked on
Determine if file has changed using Checksum and linux shell script
I have a file which I want to send out an email to users if the file changes. We want to do this via a bash shell script. I know that the best way to do this is to calculate the checksum--but can someone give me some help on how to construct the script?
ASKER
Do I run the first line outside of the script? I need the script to run continuously so any new changes are picked up.
Yes, the 1st line should run outside the script.
ASKER
Yes but if that's the case do I only have to run it once at the terminal? What about the fact that I need the script to run continuously, always emailing for changes?
If you want to have a reference then check for changes, then you run it once outside the script. This will work for static file that you don't change.
But if your aim is to check and send email each time the file changes, then the script can be like:
#!/usr/bin/bash
file=/path/to/my/file
/usr/bin/sum $file > $file.sum
grep "`cat /path/to/my/file.orig`" $file.sum
if test $? -ne 0
then
/usr/bin/mailx-s "$file changed" user@domain.com
/usr/bin/mv $file.sum /path/to/my/file.orig
fi
But if your aim is to check and send email each time the file changes, then the script can be like:
#!/usr/bin/bash
file=/path/to/my/file
/usr/bin/sum $file > $file.sum
grep "`cat /path/to/my/file.orig`" $file.sum
if test $? -ne 0
then
/usr/bin/mailx-s "$file changed" user@domain.com
/usr/bin/mv $file.sum /path/to/my/file.orig
fi
ASKER
Ok, this is isn't quite working as expected.
First of all I'm not sure mailx is configured correctly--we've never configured this machine to send mail--would prefer to use a command that lets us use a remote SMTP server.
Second, just by replacing the line that does the emailing with an echo to say file has changed, every time I run the script it echoes out "file has changed" even when nothing has happened to the file
First of all I'm not sure mailx is configured correctly--we've never configured this machine to send mail--would prefer to use a command that lets us use a remote SMTP server.
Second, just by replacing the line that does the emailing with an echo to say file has changed, every time I run the script it echoes out "file has changed" even when nothing has happened to the file
#!/bin/bash
file=/home2/dir/config/Config.xml
/usr/bin/sum $file > $file.sum
grep "'cat /home2/dir/config/Config.orig'" $file.sum
if test $? -ne 0
then
#/bin/mailx -s "$file changed" user@domain.com
echo "file has changed"
/bin/mv $file.sum /home2/dir/config/Config.orig
fi
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok this worked, but following on the advice above I used a perl script here http://caspian.dotconf.net/menu/Software/SendEmail/ to send the email, that way I could use a remote SMTP server. Thanks for all the help.
First, run
sum /path/tp/my/file > /path/to/my/file.orig
Then, use the script
#!/usr/bin/bash
file=/path/to/my/file
/usr/bin/sum $file > $file.sum
grep "`cat /path/to/my/file.orig`" $file.sum
if test $? -ne 0
then
/usr/bin/mailx-s "$file changed" user@domain.com
fi