1

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/* [email protected]:/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" [email protected] < ~/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!

1
  • Do you want to send a mail every time the mv command moved a file from the watched directory?
    – lgarzo
    Jun 25, 2012 at 18:33

3 Answers 3

1

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" [email protected] < ~/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.

3
  • I'm going to make some tests and I'll post here the results asap thks!
    – DJames
    Jun 27, 2012 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, 2012 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, 2012 at 18:29
1

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/* [email protected]:/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" [email protected] < ~/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!

-1
zenity --warning --text "my text" &

run as a background command, non blocking

1
  • 1
    Could you please explain how that is going to solve the problem in question? Consider the issues that the other answers address which also explain my scepticism of your answer. -1 Mar 10, 2018 at 10:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .