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.
在我的linux框中有一个名为images的文件夹。这个文件夹连接到一个网站,网站管理员可以向这个网站添加图片。然而,当添加图片时,我需要一个命令来运行调整所有图片的大小。
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.
简而言之,我想知道如何在向特定位置添加新文件时让服务器运行特定的命令。
6 个解决方案
#1
22
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.
我不知道人们是如何将内容上传到这个文件夹的,但是您可能希望使用比使用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,并且您可以访问FTP服务器的日志,我建议跟踪该日志以查看成功的上传。与使用传统cron的轮询方法相比,这种事件触发方法将更快、更可靠、负载更少,而且与使用inotify的方法相比,这种方法更易于移植和调试。
The way you handle this will of course depend on your FTP server. I have one running vsftpd whose logs include lines like this:
当然,您处理这个问题的方式取决于您的FTP服务器。我有一个正在运行的vsftpd,它的日志中有这样的行:
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"
The UPLOAD
line only gets added when vsftpd has successfully saved the file. You could parse this in a shell script like this:
只有当vsftpd成功保存文件时,才添加上传行。您可以在shell脚本中对其进行如下解析:
#!/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
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
.
如果您使用的是HTTP上传工具,请查看该工具是否有用于记录传入文件的文本日志文件。如果它不考虑向它添加某种日志记录器函数,那么它将生成可以跟踪的日志。
#2
4
Like John commented the inotify
API is a starting point, however you might be interested in some tools that use this API to perform file notification tasks:
像John评论的一样,inotify API是一个起点,但是你可能会对一些使用这个API来执行文件通知任务的工具感兴趣:
For example incron which can be used to run cron-like tasks when file or directory changes are detected.
例如,incron可以在检测到文件或目录更改时用于运行类似cron的任务。
Or inotify-tools which is a set of command-line tools that you could use to build your own file notification shell script.
或者inotify-tools,它是一组命令行工具,您可以使用它们构建自己的文件通知shell脚本。
If you look at the bottom of the Wiki pake for inotify-tools you will see references to even more tools and support for higher-level languages like Python, Perl or Ruby (example code).
如果您查看了inotify工具的Wiki pake的底部,您将看到更多的工具和对高级语言的支持,如Python、Perl或Ruby(示例代码)。
#3
3
You might want to look at inotify
你可能想看看inotify
The inotify API provides a mechanism for monitoring file system events. Inotify can be used to monitor individual files, or to monitor directories. When a directory is monitored, inotify will return events for the directory itself, and for files inside the directory.
inotify API提供了一种监视文件系统事件的机制。Inotify可以用来监视单个文件,或者监视目录。当监视一个目录时,inotify将返回目录本身和目录内文件的事件。
#4
0
Using ghotis work
使用ghotis工作
Here is what I did to get the users free space:
以下是我为用户提供的免费空间:
#!/bin/bash
tail -F -n 1 /var/log/vsftpd.log | while read line; do
if echo "$line" | grep -q 'OK LOGIN:'; then
pid=$(sed 's/.*\[\([^]]*\)\].*/\1/g' <<< "$line")
#the operator '<<<' doesnt exist in dash so use bash
if [[ $pid != *"pid"* ]]; then
echo -e "Disk 1: Contains Games:\n" > /home/vftp/"$pid"/FreeSpace.txt; df -h /media/Disk1/ >> /home/vftp/"$pid"/FreeSpace.txt
echo -e "\r\n\r\nIn order to read this properly you need to use a text editor that can read *nix format files" >> /home/vftp/"$pid"/FreeSpace.txt
fi
echo "checked"
# awk '{ sub("\r$", ""); print }' /home/vftp/"$pid"/FreeSpace.txt > /home/vftp/"$pid"/FreeSpace.txt
fi
done
#5
0
If the file is added through an HTTP upload, and if your server is apache, you might want to check mod_security.
如果文件是通过HTTP上载添加的,如果您的服务器是apache,您可能需要检查mod_security。
It enables you to run a script for every upload made through HTTP POST.
它允许您为通过HTTP POST进行的每次上传运行脚本。
#6
0
#!/bin/bash
tail -F -n0 /var/log/vsftpd.log | while read line; do
if echo "$line" | grep -q 'OK UPLOAD:'; then
filename=$(echo $line | cut -d, -f2 |awk '{print $1}')
filename="${filename%\"}"
filename="${filename#\"}"
#sleep 1s
if [ -s $filename ]; then
# do something with $filename
echo $filename
fi
fi
done
#1
22
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.
我不知道人们是如何将内容上传到这个文件夹的,但是您可能希望使用比使用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,并且您可以访问FTP服务器的日志,我建议跟踪该日志以查看成功的上传。与使用传统cron的轮询方法相比,这种事件触发方法将更快、更可靠、负载更少,而且与使用inotify的方法相比,这种方法更易于移植和调试。
The way you handle this will of course depend on your FTP server. I have one running vsftpd whose logs include lines like this:
当然,您处理这个问题的方式取决于您的FTP服务器。我有一个正在运行的vsftpd,它的日志中有这样的行:
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"
The UPLOAD
line only gets added when vsftpd has successfully saved the file. You could parse this in a shell script like this:
只有当vsftpd成功保存文件时,才添加上传行。您可以在shell脚本中对其进行如下解析:
#!/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
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
.
如果您使用的是HTTP上传工具,请查看该工具是否有用于记录传入文件的文本日志文件。如果它不考虑向它添加某种日志记录器函数,那么它将生成可以跟踪的日志。
#2
4
Like John commented the inotify
API is a starting point, however you might be interested in some tools that use this API to perform file notification tasks:
像John评论的一样,inotify API是一个起点,但是你可能会对一些使用这个API来执行文件通知任务的工具感兴趣:
For example incron which can be used to run cron-like tasks when file or directory changes are detected.
例如,incron可以在检测到文件或目录更改时用于运行类似cron的任务。
Or inotify-tools which is a set of command-line tools that you could use to build your own file notification shell script.
或者inotify-tools,它是一组命令行工具,您可以使用它们构建自己的文件通知shell脚本。
If you look at the bottom of the Wiki pake for inotify-tools you will see references to even more tools and support for higher-level languages like Python, Perl or Ruby (example code).
如果您查看了inotify工具的Wiki pake的底部,您将看到更多的工具和对高级语言的支持,如Python、Perl或Ruby(示例代码)。
#3
3
You might want to look at inotify
你可能想看看inotify
The inotify API provides a mechanism for monitoring file system events. Inotify can be used to monitor individual files, or to monitor directories. When a directory is monitored, inotify will return events for the directory itself, and for files inside the directory.
inotify API提供了一种监视文件系统事件的机制。Inotify可以用来监视单个文件,或者监视目录。当监视一个目录时,inotify将返回目录本身和目录内文件的事件。
#4
0
Using ghotis work
使用ghotis工作
Here is what I did to get the users free space:
以下是我为用户提供的免费空间:
#!/bin/bash
tail -F -n 1 /var/log/vsftpd.log | while read line; do
if echo "$line" | grep -q 'OK LOGIN:'; then
pid=$(sed 's/.*\[\([^]]*\)\].*/\1/g' <<< "$line")
#the operator '<<<' doesnt exist in dash so use bash
if [[ $pid != *"pid"* ]]; then
echo -e "Disk 1: Contains Games:\n" > /home/vftp/"$pid"/FreeSpace.txt; df -h /media/Disk1/ >> /home/vftp/"$pid"/FreeSpace.txt
echo -e "\r\n\r\nIn order to read this properly you need to use a text editor that can read *nix format files" >> /home/vftp/"$pid"/FreeSpace.txt
fi
echo "checked"
# awk '{ sub("\r$", ""); print }' /home/vftp/"$pid"/FreeSpace.txt > /home/vftp/"$pid"/FreeSpace.txt
fi
done
#5
0
If the file is added through an HTTP upload, and if your server is apache, you might want to check mod_security.
如果文件是通过HTTP上载添加的,如果您的服务器是apache,您可能需要检查mod_security。
It enables you to run a script for every upload made through HTTP POST.
它允许您为通过HTTP POST进行的每次上传运行脚本。
#6
0
#!/bin/bash
tail -F -n0 /var/log/vsftpd.log | while read line; do
if echo "$line" | grep -q 'OK UPLOAD:'; then
filename=$(echo $line | cut -d, -f2 |awk '{print $1}')
filename="${filename%\"}"
filename="${filename#\"}"
#sleep 1s
if [ -s $filename ]; then
# do something with $filename
echo $filename
fi
fi
done