Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I've tried several times to test my script, but with no success. This is the script:

zenity --question --text "my text"
if [ $? -eq 1 ]; then
    zenity --warning --text "my text"
else
    ping -n -c1 192.168.180.112
    # print result (0 se existir)
    echo $?
    if [ $? -eq 0 ]; then
        scp -r ~/Documents/Processo/CONCLUIDO/* posto-ensaios@192.168.180.112:/home/posto-ensaios/Documents/Processo
        fileName=$(inotifywait -e moved_from /home/posto-11v/Documents/Processo/CONCLUIDO | sed -r 's/^.*MOVED_FROM(,ISDIR)*\s+(.*)$/\2/g')
        mail -s "$fileName" USER@MAIL.HOST < ~/Documents/personaproject/Programa/scripts/mail.txt
        mv --backup ~/Documents/Processo/CONCLUIDO/* ~/Documents/personaproject/processos_terminados    
        zenity --info --text="my text"
    else
        zenity --warning --text "my text"
    fi
fi

I've tried to run with the sleep command with "pipe" after sed and to change the order of the commands. When running the script in their terminal the problem is that inotify always keeps waiting for an action...

I need to guarantee that files are copied, moved and the $filename is sent in email...

Any help?
Thanks!

share|improve this question
    
Do you want to send a mail every time the mv command moved a file from the watched directory? –  lgarzo Jun 25 '12 at 18:33
add comment

2 Answers 2

up vote 0 down vote accepted

If you'd like to send a mail everytime a file has been moved from the watched directory, you'll need to set up a different process monitoring this and sending the mail.

For the reasons you have just described, it doesn't matter where you put the inotifywait command, since the mv command does not happen at the same time, you won't catch the required event.

But if you watched the directory from another process, the moving takes place while you're watching and you can detect any changes.

By building on the previous example, you can start with the following sample script.

while true
do
    fileName=$(inotifywait -e moved_from /home/posto-11v/Documents/Processo/CONCLUIDO | sed -r 's/^.*MOVED_FROM(,ISDIR)*\s+(.*)$/\2/g')
    mail -s "$fileName" USER@MAIL.HOST < ~/Documents/personaproject/Programa/scripts/mail.txt
done

Please fill in the required details, it works basically the same as the previous example.

Note:
There are a few caveats, for example it might miss some moves while the mail is sent. It depends on the quantity and frequency of moves.

share|improve this answer
    
I'm going to make some tests and I'll post here the results asap thks! –  DJames Jun 27 '12 at 18:37
    
here's what I've done: created a new script called mail.sh to monitor the folder and send an email. Then added one line in my initial script pointing to my mail.sh script. hope made it clear to those like me who doesn't understand much of shell scripting. –  DJames Jun 28 '12 at 18:23
    
@Djames please note, that this method is error prone due to the fact that while you're detecting a move event and sending the mail, another move could happen that goes unnoticed. Another problem is the amount of sent mail: it is now easy to slow the mail server to a crawl if it is not powerful enough. –  lgarzo Jun 28 '12 at 18:29
add comment

here's how it's done now (initial script):

zenity --question --text "my text"
if [ $? -eq 1 ]; then
    zenity --warning --text "my text"
else
    ping -n -c1 192.168.180.112
echo $?
if [ $? -eq 0 ]; then
    scp -r ~/Documents/Processo/CONCLUIDO/* posto-ensaios@192.168.180.112:/home/posto-ensaios/Documents/Processo
    nohup ~/Documents/personaproject/Programa/scripts/mail.sh &
    sleep 1
    mv --backup ~/Documents/Processo/CONCLUIDO/* ~/Documents/personaproject/processos_terminados    
    zenity --info --text="my text"
else
    zenity --warning --text "my text"
fi
fi

then my new "mail.sh" script to monitor the folder and send the email in separate:

while true
do
    fileName=$(inotifywait -e moved_from /home/posto-11v/Documents/Processo/CONCLUIDO | sed -r 's/^.*MOVED_FROM(,ISDIR)*\s+(.*)$/\2/g')
    mail -s "$fileName" user@mail.host < ~/Documents/personaproject/Programa/scripts/mail.txt
done

Like you said, there are some caveats, if there's more than one file, only one email is sent with the first folder on the subject. That's an improvement to be made. Although this is a script to be executed by user, about once a week and probably with only with one folder.

thks!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.