linux – 在创建文件时执行bash脚本

前端之家收集整理的这篇文章主要介绍了linux – 在创建文件时执行bash脚本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在寻找一个小的bash脚本,在启动时,查看任何新创建的文件的目录.如果出现新文件,我希望它的存在触发第二个脚本运行.

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

目前我的代码如下所示:

#!/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

有没有更容易/更清洁/更少内存密集的方式来做到这一点?

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

解决方法

incron怎么样?它触发文件/目录更改命令.
sudo apt-get install incron

例:

<path> <mask> <command>

其中< path>可以是一个目录(意思是直接在该目录中的目录和/或文件(不是该目录的子目录中的文件!))或文件.

<掩模>可以是以下之一:

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 (*)

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

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

如果你看一个目录,那么$@保存目录路径,$#保存触发事件的文件.如果您观看文件,则$@保存文件的完整路径,$#为空.

工作实例:

$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文件添加以下行:

kernel /boot/vmlinuz-2.6.26-1-686 root=/dev/sda1 ro inotify=yes
原文链接:https://www.f2er.com/linux/395121.html

猜你在找的Linux相关文章