raspberryfish
Posts: 16
Joined: Wed May 23, 2012 6:13 pm

Monitoring for a directory and email-notification

Sun Oct 28, 2012 8:09 pm

Hello,

i want to use this shell script, when a directory has changed.

Code: Select all

#!/bin/bash
NOW=$(date +%Y%m%d%H%M)
HOST=$(hostname)
echo $NOW 
FILE=/var2/backup/HomeManagement/$NOW.HomeManagement.tar
tar -czf $FILE /var/www/HomeManagement
How can i add monitoring to a specific directory and run this script when something has changed? And how can i send an email notification?

thanks
Peter


raspberryfish
Posts: 16
Joined: Wed May 23, 2012 6:13 pm

Re: Monitoring for a directory and email-notification

Wed Oct 31, 2012 9:00 pm

thanks..... after some tries it finally works :)

sjfaustino
Posts: 87
Joined: Tue Jun 12, 2012 5:21 pm

Re: Monitoring for a directory and email-notification

Thu Nov 01, 2012 3:19 pm

raspberryfish wrote:thanks..... after some tries it finally works :)
You could share the answer here... That way if anyone wants to do the same in the future, they can base it on your solution.

raspberryfish
Posts: 16
Joined: Wed May 23, 2012 6:13 pm

Re: Monitoring for a directory and email-notification

Thu Nov 01, 2012 7:30 pm

of course,... didnt think it would be that interessting

well this my shell-script "check.sh" to apply notification

Code: Select all

#!/bin/bash
while inotifywait -e modify /var2/ftpupload/HomeManagement; do
	/var2/testbash/dateprint.sh
done
and this script i put in /etc/init.d to run the script above during system boot

Code: Select all

#!/bin/bash
# /etc/init.d/checkupload.sh
 
# Carry out specific functions when asked to by the system
case "$1" in
  start)
    ./var2/testbash/check.sh
    ;;
  stop)
    pkill inotify
    echo "stopped"
    ;;
  *)
    echo "Usage: /etc/init.d/checkupload.sh {start|stop}"
    exit 1
    ;;
esac
 
exit 0
and well there is dateprint.sh performs all actions

Code: Select all

#!/bin/bash
sleep 30
TODAY=$(date +%Y-%m-%d)
NOW=$(date +%Y%m%d%H%M)
FILE=/var2/backup/HomeManagement/$NOW.HomeManagement.tar
tar -czf $FILE /var/www/HomeManagement 
cp -R /var2/ftpupload/HomeManagement /var/www/
sleep 30
echo "Sending mail..."

sudo mutt -s "Directory has been updated" xxx@yyy.com -a $FILE < email.txt
echo "Mail sent"
sudo /etc/init.d/nginx reload
exit 0;
hope this helps

Return to “Python”