0

I have the script below running as a deamon. It checks for new .gz files in a upload file. moves it to the correct location en gzip's it.

When i run the script (as root (bad me)) by hand it works. when the deamon runs it gives the error

Dec 22 16:56:06 server watchftp.script[131856]: gzip: /var/www/html/config/MER2-SRX.conf.gz: unexpected end of file

I don't see where the problem is. anyone else??

Script code:

#!/bin/bash

savedir='/var/www/html/config/'

inotifywait -m /srv/ftp/upload -e create -e moved_to  |
while read dir action file; do
        echo "The file '$file' appeared in directory '$dir' via '$action'"
        #set correct permissions
        chown root:root $dir$file
        chmod 755 $dir$file

        #create new filename and subdirectory name for save location
        newfilename=$( echo -n $file | cut -d '_' -f 1)
        parkdir=$( echo -n $newfilename | cut -d '-' -f 1)

        mv $dir$file $savedir$newfilename.conf.gz

        #check if subdirectory exist otherwise create it
        if [[ ! -d $savedir$parkdir ]]; then
                echo "create directory $savedir$parkdir"
                mkdir $savedir$parkdir
        fi
        gunzip -dc $savedir$newfilename.conf.gz > $savedir$parkdir/$newfilename.conf
done
Jeebeevee
  • 13
  • 3
  • I'm guessing you are trying to read it before the writer has written out all (or possibly even any) of it. Try to somehow wait until it's no longer open (`lsof`) or look at its size to figure out when it's no longer growing. – tripleee Dec 22 '21 at 16:16
  • As an aside, probably review [When to wrap quotes around a shell variable?](https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable) – tripleee Dec 22 '21 at 16:17
  • 1
    Also, don't use `echo -n`. In some versions, that'll print the string(s) without a newline at the end (which you don't really want here), but in other versions it'll print "-n" at the beginning of the line (which you *really* don't want). Actually, I'd replace the entire `cut` thing with a built-in substitution: `newfilename="${file%%_*}"`. Also, `mv` without `-f` or `-i` always makes me nervous, since it'll silently delete any previous file with the new name. – Gordon Davisson Dec 22 '21 at 22:04
  • thanks for the comments. I will read about the qoutes @tripleee. – Jeebeevee Dec 23 '21 at 07:38
  • Probably try http://shellcheck.net/ to capture more of these common beginner errors, including several which Gordon pointed out. – tripleee Dec 23 '21 at 07:39
  • @GordonDavisson the mv without -i are intentional (i want it to delete/overwrite older/existing files). the -f I have add it after i posted here (a minor issue) Also thanks for the substitution option, i will use that. – Jeebeevee Dec 23 '21 at 07:48
  • @tripleee thx for shellcheck.net i didn't know that one. – Jeebeevee Dec 23 '21 at 07:49

1 Answers1

2

You're processing a partially-written file, because the create event will trigger your script as soon as the file is created, not when they've finished writing it.

Use the close_write event instead. This is triggered when the writer finishes writing the file and closes it. It assumes that they don't write the file in partial batches, but that would be very unusual.

So change -e create to -e close_write.

Barmar
  • 741,623
  • 53
  • 500
  • 612