User avatar
ekla
Posts: 47
Joined: Wed Apr 09, 2014 9:39 am
Location: Lyon, France

BASH / Run a shell command when a file added / pb

Thu Mar 26, 2015 10:44 am

Hi all, thanks to take time to read this.

I want to do something special : when a file is added (by ftp) to a specific folder, a script launch a command. I found a good script but maybe i made a mistake and i don't understand fully what each line said.

the trick is to watch /var/log/vsftpd.log
when a file is uploaded correctly, vsftpd return a line with "OK UPLOAD"

Code: Select all

tail -f /var/log/vsftpd.log | while read line; do
  if echo "$line" | grep -q "OK UPLOAD:"; then
    filename=$(echo "$line" | cut -d, -f2)
    if [ -s "$filename" ]; then
        echo "hello"
 fi
  fi
done
why it doesn't print hello when i upload a file ?
and i want to print hello also when the file uploaded contained RG14 in its filename.

If someone call help me, i spent all night on that...

Thanks
our fears are like dragons guarding our most precious treasures

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: BASH / Run a shell command when a file added / pb

Thu Mar 26, 2015 11:58 am

Looks like you stole the code from http://stackoverflow.com/questions/1095 ... e-is-added

If your log has a line
Fri May 25 07:36:12 2012 [pid 94380] [joe] OK UPLOAD: Client "10.8.7.16", "/path/to/file.zip", 8395136 bytes, 845.75Kbyte/sec

Why not parse that with grep or awk.
awk '{split ($0,a,"OK UPLOAD"); print a[2]}' < /var/log/vsftpd.log | cut -d, -f2
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

cdo
Posts: 9
Joined: Tue Nov 19, 2013 5:13 pm

Re: BASH / Run a shell command when a file added / pb

Thu Mar 26, 2015 12:01 pm

Another option would be using inotifywait, as suggested here:
https://unix.stackexchange.com/question ... -new-files
I never tried this, but maybe it's worth a look.

User avatar
ekla
Posts: 47
Joined: Wed Apr 09, 2014 9:39 am
Location: Lyon, France

Re: BASH / Run a shell command when a file added / pb

Thu Mar 26, 2015 12:05 pm

Hi Dougie, yes you right ! stole nooooo just want to understand it ! :lol:
As i'm so new in bash, i want to understand fully what every lines means.

why hello is not print when i upload a file ?

ok i will try your line. Thanks Dougie
our fears are like dragons guarding our most precious treasures

User avatar
ekla
Posts: 47
Joined: Wed Apr 09, 2014 9:39 am
Location: Lyon, France

Re: BASH / Run a shell command when a file added / pb

Thu Mar 26, 2015 12:06 pm

cdo wrote:Another option would be using inotifywait, as suggested here:
https://unix.stackexchange.com/question ... -new-files
I never tried this, but maybe it's worth a look.
yes i did that but with no success. i spent all night on that too.
Thanks
our fears are like dragons guarding our most precious treasures

User avatar
DougieLawson
Posts: 39124
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: BASH / Run a shell command when a file added / pb

Thu Mar 26, 2015 12:55 pm

This will run forever

Code: Select all

#!/bin/bash
FILE=""
while [ "$FILE" == "" ]; do
FILE=$(tail -1 /var/log/vsftpd.log | awk '{split ($0,a,"OK UPLOAD"); print a[2]}' | cut -d, -f2)
  if [ "$FILE" != "" ]; then
    echo "Filename:"$FILE" uploaded"
    FILE=""
  fi
done
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
ekla
Posts: 47
Joined: Wed Apr 09, 2014 9:39 am
Location: Lyon, France

Re: BASH / Run a shell command when a file added / pb

Thu Mar 26, 2015 1:28 pm

DougieLawson wrote:This will run forever

Code: Select all

#!/bin/bash
FILE=""
while [ "$FILE" == "" ]; do
FILE=$(tail -1 /var/log/vsftpd.log | awk '{split ($0,a,"OK UPLOAD"); print a[2]}' | cut -d, -f2)
  if [ "$FILE" != "" ]; then
    echo "Filename:"$FILE" uploaded"
    FILE=""
  fi
done
As usual Dougie you have the answer. Thanks
I don't want to disturb you again because what i want to do is more complicated.
So really thanks to you

What i want to do is :
When a file like PG14_TR_UK-ST.MP4 is uploaded (completely) to the specific folder via FTP, the script copy paste the file to a folder called PG14 and create 2 others folders LQ and HQ. After the copy paste the script delete the file on the FTP folder and launch an avconv job to encode the file with specific options compared part of the name of the file. When finished it sends a mail to say "hey dude your file is ready to download". Other files like PG14_TR_FR-ST.MP4 are uploaded and the script knows that there is already a folder PG14 so it copy paste, delete and do another avconv job, and mail notification. And again and again with new files PG15,...

so first > MORE COFFEE
our fears are like dragons guarding our most precious treasures

Return to “Other programming languages”