当添加一个文件运行shell命令 [英] Run a shell command when a file is added

查看:147
本文介绍了当添加一个文件运行shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的Linux操作系统中的文件夹命名的图像。
此文件夹连接到一个网站,该网站的管理员有权图片添加到这个网站的能力。然而,在添加图片,我想一个命令来运行调整所有的图片。一个目录

I have a folder named images on my linux box. This folder is connected to a website and the admin of the site has the ability to add pictures to this site. However, when a picture is added, I want a command to run resizing all of the pictures a directory.

总之,我想知道我怎样才能使服务器运行特定的命令时,一个新的文件添加到特定的位置。

In short, I want to know how I can make the server run a specific command when a new file is added to a specific location.

推荐答案

我不知道人们是如何将内容上传到该文件夹​​,但你可能想使用一些低技术含量比监测与inotify的目录。

I don't know how people are uploading content to this folder, but you might want to use something lower-tech than monitoring the directory with inotify.

如果该协议FTP,你可以访问你的FTP服务器的日志,我建议拖尾的日志看成功上传。这种类型的事件触发方式会更快,更可靠,比传统的cron轮询方法负载更小,更便携,更简单。使用inotify的不是一些调试。

If the protocol is FTP and you have access to your FTP server's log, I suggest tailing that log to watch for successful uploads. This sort of event-triggered approach will be faster, more reliable, and less load than a polling approach with traditional cron, and more portable and easier to debug than something using inotify.

您处理,当然这将取决于这样您的FTP服务器上。我有一个运行 vsftpd的其日志包括这样几行:

The way you handle this will of course depend on your FTP server. I have one running vsftpd whose logs include lines like this:

Fri May 25 07:36:02 2012 [pid 94378] [joe] OK LOGIN: Client "10.8.7.16"
Fri May 25 07:36:12 2012 [pid 94380] [joe] OK UPLOAD: Client "10.8.7.16", "/path/to/file.zip", 8395136 bytes, 845.75Kbyte/sec
Fri May 25 07:36:12 2012 [pid 94380] [joe] OK CHMOD: Client "10.8.7.16", "/path/to/file.zip 644"

上传行仅当vsftpd的成功保存的文件被添加。你可以在这样的shell脚本解析这样的:

The UPLOAD line only gets added when vsftpd has successfully saved the file. You could parse this in a shell script like this:

#!/bin/sh

tail -F /var/log/vsftpd.log | while read line; do
  if echo "$line" | grep -q 'OK UPLOAD:'; then
    filename=$(echo "$line" | cut -d, -f2)
    if [ -s "$filename" ]; then
      # do something with $filename
    fi
  fi
done

如果您使用的是HTTP上传工具,看看是否有工具,使用它来记录输入的文件文本的日志文件。如果不考虑加入一些记录功能给它的,所以它会产生日志,你可以

If you're using an HTTP upload tool, see if that tool has a text log file it uses to record incoming files. If it doesn't consider adding some sort of logger function to it, so it'll produce logs that you can tail.

这篇关于当添加一个文件运行shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆