简体   繁体   English

Bash 脚本:给出错误:gzip:<file> .conf.gz:文件意外结束</file>

[英]Bash script: gives error: gzip: <File>.conf.gz: unexpected end of file

I have the script below running as a deamon.我将下面的脚本作为守护进程运行。 It checks for new.gz files in a upload file.它检查上传文件中的 new.gz 文件。 moves it to the correct location en gzip's it.将它移动到正确的位置 en gzip's it。

When i run the script (as root (bad me)) by hand it works.当我手动运行脚本(以root身份(坏我))时,它可以工作。 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

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.您正在处理一个部分写入的文件,因为create事件将在文件创建后立即触发您的脚本,而不是在他们完成写入时触发。

Use the close_write event instead.请改用close_write事件。 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 .所以将-e create更改为-e close_write

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM