访问 URL 时触发操作

Bas*_*asj 1 grep cron logs email apache-httpd

假设我向某人发送了一封电子邮件,其中包含指向我网站的链接,我真的希望他会访问它(手指交叉样式):

http://www.example.com/?utm_source=email392
Run Code Online (Sandbox Code Playgroud)

或者

http://www.example.com/somefile.pdf?utm_source=email392
Run Code Online (Sandbox Code Playgroud)

如何通过定期检查使 Linux 在访问此 URL 时触发操作(例如向自己发送自动电子邮件)/var/log/apache2/other_vhosts_access.log

我无法在 PHP 级别执行此操作,因为我需要为各种来源/网站执行此操作(其中一些使用 PHP,有些不使用并且只是指向要下载的文件的链接等;即使对于使用 PHP 的网站) ,我不想修改每一个index.php从那里做,这就是为什么我更喜欢 Apache 日志解析方法)

seb*_*sth 5

使用 bash 进程替换进行实时日志监控:

#!/bin/bash

while IFS='$\n' read -r line;
do
    # action here, log line in $line

done < <(tail -n 0 -f /var/log/apache2/other_vhosts_access.log | \
         grep '/somefile.pdf?utm_source=email392')
Run Code Online (Sandbox Code Playgroud)

进程替换为读取循环提供内部管道的输出<(...)。日志行本身被分配给变量$line

使用 监视日志tail -f,它在将行写入日志时输出行。如果您的日志文件由logrotate定期移动,请添加--follow=name--retry选项以查看文件路径,而不仅仅是文件描述符。

来自tail 的输出通过管道传输到grep,这将过滤与您的 URL 匹配的相关行。