在创建文件时执行 bash 脚本 [英] Executing a bash script upon file creation
问题描述
我正在寻找编写一个小的 bash 脚本,以便在启动时监视任何新创建文件的目录.如果出现新文件,我希望它的存在触发第二个脚本运行.
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.
我看到这被用来触发压缩最近数字化的视频,并将其添加到摄取的镜头日志中.
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=$1
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?
编辑我将更改每个捕获会话的摄取目录.我已经相应地调整了代码
EDIT I will be changing the ingestion directory per capture session. I have adjusted the code accordingly
推荐答案
incron 怎么样?它触发文件/目录更改命令.
How about incron? It triggering Commands On File/Directory Changes.
sudo apt-get install incron
示例:
<path> <mask> <command>
其中 <path>
可以是目录(意味着该目录和/或直接在该目录中的文件(不是该目录的子目录中的文件!)被监视)或文件.
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.
可以是以下之一:
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:
$$ 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.
工作示例:
$sudo echo spatel > /etc/incron.allow
$sudo echo root > /etc/incron.allow
启动守护进程:
$sudo /etc/init.d/incrond start
编辑incrontab
文件
$incrontab -e
/home/spatel IN_CLOSE_WRITE touch /tmp/incrontest-$#
测试一下
$touch /home/spatel/alpha
结果:
$ls -l /tmp/*alpha*
-rw-r--r-- 1 spatel spatel 0 Feb 4 12:32 /tmp/incrontest-alpha
注意: 在 Ubuntu
中,您需要在启动时激活 inotify.请在 Grub menu.lst 文件中添加以下行:
Notes: In Ubuntu
you need to activate inotify at boot time. Please add following line in Grub menu.lst file:
kernel /boot/vmlinuz-2.6.26-1-686 root=/dev/sda1 ro inotify=yes
这篇关于在创建文件时执行 bash 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!