13

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.

1

6 Answers 6

25

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.

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.

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"

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

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.

3
  • This is perfect. Im actually using a php/html upload button. But I can just scan the access file from apache for the upload page.
    – Cripto
    Commented Jun 9, 2012 at 9:15
  • 1
    What I don't see here is what you're doing to trigger the script that tails the log file.
    – Jason Gray
    Commented Oct 22, 2013 at 15:21
  • 1
    @JasonGray, Are you familiar with tail's -F option? man tail for details. The -F causes tail to wait until the file grows, then send it to stdout. So the "trigger" for the <pre>do something</pre> part is simply the addition of new content to the file. This script runs once, perhaps at startup, and keeps running. How you launch it is up to you, and depends on your operating system or distro.
    – ghoti
    Commented Oct 22, 2013 at 19:34
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:

For example incron which can be used to run cron-like tasks when file or directory changes are detected.

Or inotify-tools which is a set of command-line tools that you could use to build your own file notification shell script.

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).

3

You might want to look at 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.

3
#!/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
  • I upvote because it uses tail -F -n0 that waits for new lines without processing existing ones.
    – None
    Commented Jul 16, 2020 at 13:03
1

Using ghotis work

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
0

If the file is added through an HTTP upload, and if your server is apache, you might want to check mod_security.

It enables you to run a script for every upload made through HTTP POST.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.