Home > database >  Bash script: gives error: gzip: <File>.conf.gz: unexpected end of file
Bash script: gives error: gzip: <File>.conf.gz: unexpected end of file

Time:12-24

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

CodePudding user response:

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.

  •  Tags:  
  • bash
  • Related