Move the file to another directory after transferring it

I have a video encoding script that I would like to run as soon as the file is moved to a specific directory.

If I use something like inotify, how can I guarantee that the file is not encoded before it is moved?

I thought of doing something like:

  • Copy the (rsync) file to a temporary directory.
  • After completion, move (a simple "mv") to the encoding directory.
  • Ask my script to track the encoding directory.

However, how can I make step # 2 work correctly and run only once when # 1 is complete?

I am using Ubuntu Server 11.10 and I would like to use bash, but I could convince you to use Python if this simplifies the problems.

I do not β€œupload” files to this directory on my own; rather, I will use rsync the vast majority of the time.

In addition, this Ubuntu server runs on a virtual machine.

I have my main file repository mounted via NFS from a FreeBSD server.

+6
source share
3 answers

One method that I use works with FTP. You send a command to the FTP server to transfer the file to the secondary directory. As soon as the command completes, you send the second command to the server, this time telling it to rename the file from the aux directory to the destination destination directory. If you use inotify or directory polling, the file name will not appear until the renaming is completed, thus, you are guaranteed that the file will be completed.

I am not familiar with rsync, so I don’t know if it has a similar renaming capability.

+1
source

The easiest way is to download using a program like wget or curl , which will not complete until the file is downloaded. Otherwise, I'm not sure if this is the absolute best solution, but you can use a script that loops lsof check if the file is open with lsof and grep :

 while lsof | grep /path/to/download >/dev/null; do sleep 5; done mv /path/to/download /path/to/encode/dir 
+2
source

inotifywait looks promising. You should be a little careful, because if the download finishes before the notification is configured, it will wait forever.

 inotifywait -e close_write "$download_file" mv "$download_file" "$new_location" 
0
source

Source: https://habr.com/ru/post/913254/


All Articles