centos 无法使inron inotify工作

c2e8gylq  于 2022-11-07  发布在  其他
关注(0)|答案(2)|浏览(162)

所以在亚历克斯回答后我的步骤如下:
创建shell代码

root@ip[/]# touch mylog.sh
root@ip[/]# nano mylog.sh

复制www.example.com中的代码mylog.sh


# !/bin/bash

echo "File $1 created." >> /mylog.log

权限

root@ip[/]# chmod +x mylog.sh

创建日志文件

root@ip[/]# touch mylog.log

n.开口式扫描工作台

incrontab -e

将新命令置于

/test/ IN_CREATE mylog.sh $@$#

重新加载incron -创建新文件-检查日志文件

root@ip[/]# incrontab --reload
requesting table reload for user 'root'...
request done

root@ip[/]# cd test
root@ip[/test]# touch newfile.txt

root@ip[/test]# cd /
root@ip[/]# nano mylog.log

但仍然是空的日志文件...我错过了什么吗?
最后,使用完整路径调用shell脚本,这样就实现了:

/test/ IN_CREATE /mylog.sh $@$#
t9eec4r0

t9eec4r01#

您通常可以在**/var/log/messages**中找到incron日志
如果要将事件记录到特定文件,可以用途:

/test/ IN_CREATE mylog.sh $@$#

其中,mylog.sh是处理日志记录的shell脚本。


# !/bin/bash

echo "File $1 created." >> /home/myuser/filescreated.log

请不要忘记通过chmod +x www.example.com给予此shell脚本执行权限mylog.sh
说明:一旦你开始为你要调用的命令使用参数,你就必须把它全部放到一个shell脚本中,因为incron并不把参数传递给你的命令,而是把它解释为自己的参数。
不要忘记调用incrontab --在更改incrontab之后重新加载。

另一个例子

被包围的

/text/ IN_CREATE /home/myuser/mylog.sh $@ $#

mylog.sh


# !/bin/bash

echo "$(date) File $2 in $1 created." >> /home/myuser/log.txt
ulydmbyx

ulydmbyx2#

在 Alexandria 的Baltasar answer之后,您还可以有一个执行重定向的脚本,并使您的最终脚本不受该逻辑的影响。
低于std_wrapper.sh


# !/bin/bash

### FLAGS

set -Eeuo pipefail

### INIT SCRIPT

SCRIPT_FULLNAME=$(basename -- ${0})
usage="usage: ${SCRIPT_FULLNAME} log_file target_script target_file watched_dir event"

## ARGUMENTS

log_file="${1}"
target_script="${2}"
target_file="${3}"
watched_dir="${4}"
event="${5}"

### MAIN

if [ -z "${log_file}" ] || [ -z "${target_script}" ] || [ -z "${target_file}" ]; then
  echo "${usage}" >&2
  exit 1
fi

# do the actual call and apply the redirection:

${target_script} "${target_file}" "${watched_dir}" "${event}" >> "${log_file}" 2>&1
  • 请确保脚本可以运行($ chmod 770 std_wrapper.sh):

在您的incrontab$ incrontab -e)中:

/test/ IN_CREATE /path/std_wrapper.sh /path/log/test.create /path/actual_script.sh $# $@ $%

actual_script.sh可能如下所示:


# !/bin/bash

### FLAGS

set -Eeuo pipefail

### Input Parameters

filename="${1}"
watched_dir="${2}"
event="${3}"
full_filename="${watched_dir}${filename}"

### Main

dt="$(date '+%d/%m/%YT%H:%M:%S')"
echo "$dt (event:) $event (file:) $filename (dir:) $watched_dir <----- going to process ----->"
echo "sleeping 10 seconds..."
sleep 10
dt="$(date '+%d/%m/%YT%H:%M:%S')"
echo "$dt (event:) $event (full_filename:) $full_filename <----- returning from sleep -->"

连续创建两个文件(在10秒内)

$ touch /test/new-file && sleep 5 && touch /test/another-file

将创建如下日志:

$ cat /path/log/test.create
07/11/2022T08:00:50 (event:) IN_CREATE (file:) new-file (dir:) /test/ <----- going to process ----->
sleeping 10 seconds...
07/11/2022T08:00:55 (event:) IN_CREATE (file:) another-file (dir:) /test/ <----- going to process ----->
sleeping 10 seconds...
07/11/2022T08:01:10 (event:) IN_CREATE (full_filename:) /test/new-file <----- returning from sleep -->
07/11/2022T08:01:15 (event:) IN_CREATE (full_filename:) /test/another-file <----- returning from sleep -->

相关问题