在创建文件时执行 bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14692353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:26:56  来源:igfitidea点击:

Executing a bash script upon file creation

linuxbashloops

提问by Simianspaceman

I am looking write a small bash script to, when launched, watch a directory for any newly created files. If a new file appears, I want its presence to trigger a second script to run.

我正在寻找编写一个小的 bash 脚本,以便在启动时观察任何新创建文件的目录。如果出现新文件,我希望它的存在触发第二个脚本运行。

I see this being used to trigger the compression recently digitized video, and add it to a log of ingested footage.

我看到这被用来触发压缩最近数字化的视频,并将其添加到摄取的镜头日志中。

Currently my code looks like this:

目前我的代码是这样的:

#!/bin/sh

##VIDSTAT is a global variable coming from a parent script.
##proj is the ingestion directory coming from a parent script
proj=

dir="/home/$USER/data/movies/$proj"
dirlist=$(ls $dir)


while { $VIDSTAT -eq 1 }:
do
    for mov in $dirlist
    do
        if [ "$(( $(date +"%s") - $(stat -c "%Y" $mov) ))" -lt "5" ]
        then
        ~/bin/compressNlog.sh $mov
        fi
    done
done

Is there an easier/cleaner/less memory intensive way to do this?

有没有更简单/更清洁/更少内存密集型的方法来做到这一点?

EDITI will be changing the ingestion directory per capture session. I have adjusted the code accordingly

编辑我将更改每个捕获会话的摄取目录。我已经相应地调整了代码

回答by Satish

How about incron? It triggering Commands On File/Directory Changes.

incron怎么样?它触发文件/目录更改命令。

sudo apt-get install incron

Example:

例子:

<path> <mask> <command>

Where <path>can be a directory (meaning the directory and/or the files directly in that directory (not files in subdirectories of that directory!) are watched) or a file.

哪里<path>可以是目录(意味着目录和/或直接在该目录中的文件(不是该目录的子目录中的文件!)被监视)或文件。

<mask>can be one of the following:

<mask>可以是以下之一:

IN_ACCESS           File was accessed (read) (*)
IN_ATTRIB           Metadata changed (permissions, timestamps, extended attributes, etc.) (*)
IN_CLOSE_WRITE      File opened for writing was closed (*)
IN_CLOSE_NOWRITE    File not opened for writing was closed (*)
IN_CREATE           File/directory created in watched directory (*)
IN_DELETE           File/directory deleted from watched directory (*)
IN_DELETE_SELF           Watched file/directory was itself deleted
IN_MODIFY           File was modified (*)
IN_MOVE_SELF        Watched file/directory was itself moved
IN_MOVED_FROM       File moved out of watched directory (*)
IN_MOVED_TO         File moved into watched directory (*)
IN_OPEN             File was opened (*)

<command>is the command that should be run when the event occurs. The following wildards may be used inside the command specification:

<command>是事件发生时应该运行的命令。可以在命令规范中使用以下通配符:

$$   dollar sign
$@   watched filesystem path (see above)
$#   event-related file name
$%   event flags (textually)
$&   event flags (numerically)

If you watch a directory, then $@ holds the directory path and $# the file that triggered the event. If you watch a file, then $@ holds the complete path to the file and $# is empty.

如果您查看目录,则 $@ 保存目录路径, $# 保存触发事件的文件。如果您查看文件,则 $@ 保存文件的完整路径,而 $# 为空。

Working Example:

工作示例:

$sudo echo spatel > /etc/incron.allow
$sudo echo root > /etc/incron.allow

Start Daemon:

启动守护进程:

$sudo /etc/init.d/incrond start

Edit incrontabfile

编辑incrontab文件

$incrontab -e
/home/spatel IN_CLOSE_WRITE touch /tmp/incrontest-$#

Test it

测试一下

$touch /home/spatel/alpha

Result:

结果:

$ls -l /tmp/*alpha*
-rw-r--r-- 1 spatel spatel 0 Feb  4 12:32 /tmp/incrontest-alpha

Notes:In Ubuntuyou need to activate inotify at boot time. Please add following line in Grub menu.lst file:

注:Ubuntu你需要在启动时激活的inotify。请在 Grub menu.lst 文件中添加以下行:

kernel /boot/vmlinuz-2.6.26-1-686 root=/dev/sda1 ro inotify=yes

回答by Gilles Quenot

You can do this with the magical inotifytool :

你可以用神奇的inotify工具做到这一点:

inotifywait -r -m ./YOUR_MONITORED_DIR |
    while read a b file; do
        [[ $b == *CREATE* ]] && ./another_script "$file"
    done

This method have the big advantage to avoid polling every N seconds.

这种方法有很大的优势,可以避免每 N 秒轮询一次。

Inotify (inode notify) is a Linux kernel subsystem that acts to extend filesystems to notice changes to the filesystem, and report those changes to applications. It replaces an earlier facility, dnotify, which had similar goals.

Inotify (inode notify) 是一个 Linux 内核子系统,用于扩展文件系统以通知文件系统的更改,并将这些更改报告给应用程序。它取代了具有类似目标的早期工具 dnotify。

http://en.wikipedia.org/wiki/Inotify
See inotify doc

http://en.wikipedia.org/wiki/Inotify
查看inotify 文档

回答by Spencer Rathbun

Use iwatch. No, really. It'll handle all of the details of making a daemon, running on startup, monitor and log, so on and so on. All you need to do is set the options, and have your bash script handle the details of actually doing something with the file.

使用iwatch。不完全是。它将处理制作守护进程、启动时运行、监控和日志等的所有细节。您需要做的就是设置选项,并让您的 bash 脚本处理实际处理文件的细节。