Trigger linux script after file sync complete.


Recommended Posts

I use ResilioSync to move files from one server to another across the country.  I have a script that I wrote (that I trigger manually) that processes the file on the receiving end and moves it out of the "incoming" folder.  This part is working flawlessly.

I'm trying to figure out a way to automatically trigger my script when a new file has finished transferring via Resilio Sync.

Any suggestions? 

Link to comment
Share on other sites

What type of system(s) are these? Linux, macOS, Windows?

Should be pretty easy to periodically run a script that compares the hash of a file to a previously known value and does something when it changes. If your system is Linux you can run the script every minute with cron etc.

Here's a bash script I slapped together... you'll need the sha256sum binary, which you can get for just about any platform. See GNU coreutils or if you're on macOS and have Homebrew, `brew install coreutils`. This won't work for Windows but something similar can be done in PowerShell if needed.

#!/usr/bin/env bash

function calculateHash() {
  sha256sum "$srcfile" >"$oldhash"
  return
}

[ $# == 0 ] && { echo "usage: $0 <filename>"; exit; }

srcfile="$1"
fName="${srcfile##*/}"
oldhash="/tmp/$fName.sha256sum"

if [ ! -e "$oldhash" ]; then
  echo "creating hashfile for $fName"
  calculateHash
  exit
fi

if sha256sum -c --quiet "$oldhash"; then
  echo "hashes match, taking no action"
else
  echo "file change detected! doing stuff..."
  # do stuff here #
  calculateHash
fi

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.