Bash script for new file in ftp folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash script for new file in ftp folder
# 1  
Old 03-07-2012
Bash script for new file in ftp folder

Hello,
I'm trying to make a bash script that send me e-mail if there is any new file in my ftp folder.

Code:
cat inotify.sh
#!/bin/sh

/usr/bin/inotifywait -e create \
    -mrq /home/mrowcp | while read line; do
    echo -n "$line " >> /var/log/inotify.log
    echo `date | cut -d " " -f1-4` >> /var/log/inotify.log
done

So at the /var/log/inotify.log I've got:
Code:
/home/mrowcp/ CREATE filefile Wed Mar 7
/home/mrowcp/ CREATE filefile123 Wed Mar 7
/home/mrowcp/ CREATE newfilecreate.txt Wed Mar 7

Now the problem is in mail sending.
How Can I send each of this new created files?May be I need another script for inotify.log monitoring, and if new line is created:

Code:
tail -n 1 /var/log/inotify.log |cut -d " " -f 3


Last edited by pludi; 03-08-2012 at 04:11 AM..
# 2  
Old 03-07-2012
I would suggest a cron job that run every N mins and checks for any new entries in the inotify.log file. I will need to "remember" where in the log it has already processed and I'd suggest a dat file like /home/mrowcp/.inotify.dat.

The script can then find any new entries from after this line:

Code:
CHKFILE=/home/mrowcp/.inotify.dat
LOGFILE=/var/log/inotify.log
NOTIFY=mrowcp@domain.com
 
show_details()
{
    echo " New file(s) have arrived: "
    echo "$newfiles" | sed 's/^/    /'
}
 
# Last line to looked at - just in case a file arrives while we are working
EL=$(wc -l $LOGFILE | awk ' { print $1 }')
 
if [ -f $CHKFILE ]
then
    SL="$(cat $CHKFILE)"
    # Old position is greater than size of file - It's may have been truncated so start from begining
    [ $EL -lt $SL ] && SL=0
else
  # No remembered previous line - start from begining
  SL=0
fi
 
newfiles=$(awk -vS=$SL -vE=$EL '
   NR>E{exit}
   /^\/home\/mrowcp\/ CREATE/&&NR>=S{print $3}' $LOGFILE)
if [ -n "$newfiles" ]
then
   show_details | mail -s "new FTP files" $NOTIFY
fi
echo "$EL" > $CHKFILE


Last edited by Chubler_XL; 03-07-2012 at 09:48 PM..
# 3  
Old 03-07-2012
Thanks bro.There is some bugs like: If I upload/receive 3 or more files:

Code:
/home/mrowcp/ CREATE aaaaaaaa Thu Mar 8
/home/mrowcp/ CREATE bbbbbbbb Thu Mar 8
/home/mrowcp/ CREATE cccccccc Thu Mar 8

1st time they all are reported to me:

Code:
 New file(s) have arrived: 
      fffffffff
      aaaaaaaa
      bbbbbbbb
      cccccccc

, but when the script start again after 1 min. I receive the last one (file) like new one:


Code:
New file(s) have arrived: 
      cccccccc

And every time when script start, I receive last uploaded file, like a new.

Code:
bash -x checker.sh
+ CHKFILE=/home/mrowcp/.inotify.dat
+ LOGFILE=/var/log/inotify.log
+ NOTIFY=it@freshlogic.bg
++ awk ' { print $1 }'
++ wc -l /var/log/inotify.log
+ EL=31
+ '[' -f /home/mrowcp/.inotify.dat ']'
++ cat /home/mrowcp/.inotify.dat
+ SL=31
+ '[' 31 -lt 31 ']'
++ awk -vS=31 -vE=31 '
   NR>E{exit}
   /^\/home\/mrowcp\/ CREATE/&&NR>=S{print $3}' /var/log/inotify.log
+ newfiles=cccccccc
+ '[' -n cccccccc ']'
+ mail -s 'new FTP files' mail@test.mail.com
+ show_details
+ echo ' New file(s) have arrived: '
+ sed 's/^/    /'
+ echo cccccccc
+ echo 31

P.S. Can you give me example with mutt instead of mail.If I must attach every received file in my mail.

Last edited by pludi; 03-08-2012 at 04:13 AM..
# 4  
Old 03-07-2012
Looks like the .inotify.dat file is getting into the log file so probably best to just filter that one out.

You could also put the .inotify.dat file in another directory as the awk code only looks for files in the /home/mrowcp/ directory

Code:
CHKFILE=/home/mrowcp/.inotify.dat
LOGFILE=/var/log/inotify.log
NOTIFY=mrowcp@domain.com
 
show_details()
{
    echo " New file(s) have arrived: "
    echo "$newfiles" | sed 's/^/    /'
}
 
# Last line to looked at - just in case a file arrives while we are working
EL=$(wc -l $LOGFILE | awk ' { print $1 }')
 
if [ -f $CHKFILE ]
then
    SL="$(cat $CHKFILE)"
    # Old position is greater than size of file - It's may have been truncated so start from begining
    [ $EL -lt $SL ] && SL=0
else
  # No remembered previous line - start from begining
  SL=0
fi
 
newfiles=$(awk -vS=$SL -vE=$EL '
   NR>E{exit}
   /^\/home\/mrowcp\/ CREATE/&&NR>=S&&$3!=".inotify.dat"{print $3}' $LOGFILE)
if [ -n "$newfiles" ]
then
   show_details | mail -s "new FTP files" $NOTIFY
fi
echo "$EL" > $CHKFILE


Last edited by Chubler_XL; 03-07-2012 at 11:21 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ftp bash script appends to file using cron instead of coping new file

I have a bash script that is running a mysql query and creating a csv file with a time stamp. It then uploads that to a ftp server. Everything works great when I manually run it. But then I have a cron job set to run every monday, wednesday and friday at 5am est. When the cron job runs, it... (7 Replies)
Discussion started by: akallenberger
7 Replies

2. Shell Programming and Scripting

Shell script for FTP folder pattern

Hello, I have 3 folders on FTP server in the format yyyymmddhhmiss as below,ftp> cd /home/walgreens 250 CWD command successful ftp> ls 200 PORT command successful 150 Opening ASCII mode data connection for file list 20150708072901 20150708092901 20150708102901The above folders are in... (1 Reply)
Discussion started by: pavan_test
1 Replies

3. Programming

CGI Perl script to execute bash script- unable to create folder

Hi I have a bash script which takes parameters sh /tmp/gdg.sh -b BASE-NAME -n 1 -s /source/data -p /dest/data/archive -m ARC gdg.sh will scan the /source/data and will move the contents to /dest/data/archive after passing through some filters. Its working superb from bash I have... (0 Replies)
Discussion started by: rakeshkumar
0 Replies

4. Shell Programming and Scripting

end of file error bash ftp script

Hello kind programmers :) I am a newbie and running into an error "line 28: syntax error: unexpected end of file" on the script shown below. Any help would be greatly appreciated. Thanks! #! /bin/bash if ($#argv <3) then echo 'Usage get_modis_snow ' echo 'ftp script for MYD10A2... (2 Replies)
Discussion started by: cmshreve
2 Replies

5. UNIX for Dummies Questions & Answers

Bash script to rename all files within a folder...

Hi. I don't have any experience with making scripts in bash. I need a simple script to rename all files in a folder to the format file1.avi, file2.avi, file3.avi, and so on..... Please note that the original files have different filenames and different extensions. But they all need to be... (2 Replies)
Discussion started by: dranzer
2 Replies

6. Shell Programming and Scripting

FTP from Unix Shell script to Windows Shared folder ?

Hi Before Posting my query in this forum, I have gone through various similar postings to get some idea on ftp and how to do that from unix shell script to windows server. My question is related to FTP'ing from Unix to windows shared folder My basic question is 1. Is it possible to do FTP... (4 Replies)
Discussion started by: shekharjchandra
4 Replies

7. Shell Programming and Scripting

Shell Script to monitor folder and upload found files via FTP

Hi everyone! I'm in a need of a shell script that search for all files in a folder, move all those files to a temp folder, and upload those files via FTP. When the file transfer via FTP completes successfully, the file is moved to a completed folder. In case any of those files fails, the file... (4 Replies)
Discussion started by: pulsorock
4 Replies

8. Shell Programming and Scripting

Bash script to delete folder based on text file information

I have been working on a script to list all the name's of a subfolder in a text file then edit that text file and then delete the subfolder base on the edited text file so far I have been able to do every thing I just talked about but can't figure out how to delete the subfolers base on a text file... (8 Replies)
Discussion started by: bone11409
8 Replies

9. Shell Programming and Scripting

moving a file to a new folder and automated ftp

how to move a file to a different folder after an automated FTP . (1 Reply)
Discussion started by: dineshr85
1 Replies

10. Shell Programming and Scripting

Parse the .txt file for folder name and FTP to the corrsponding folder.

Oracle procedure create files on UNIX folder on a regular basis. I need to FTP files onto windows server and place the files, based on their name, in the corresponding folders. File name is as follows: ccyymmddfoldernamefile.txt; Folder Name length could be of any size; however, the prefix and... (3 Replies)
Discussion started by: MeganP
3 Replies
Login or Register to Ask a Question