Dirk's Tech Findings

rsync: Run script on remote host after rsync execution

Publication date: 2020-06-30

Issue: I wanted to execute a command after a successful rsync invocation

Solution: Create a wrapper script around the rsync executable

Create a script wrapping rsync on the destination host. I executes arbitrary commands (example here: "touch /tmp/rsync-after"). stdin, stdout, stderr are redirected to avoid confusing any other scripts that rely on usual rsync behavior.

#!/bin/bash
/usr/bin/rsync "$@"
result=$?
(
  if [ $result -eq 0 ]; then
     touch /tmp/rsync-after;
  fi
) >/dev/null 2>/dev/null </dev/null

exit $result

Make the script executable.

On the source host, this script needs to be specified using the "rsync-path" parameter so that it gets used as rsync executable on the destination host:

rsync -a --rsync-path=/path_to_script/rsyncafter.sh localfile desthost:

Hint towards the solution

This is based on a script that was published for lsyncd. Link: http://axkibe.github.io/lsyncd/faq/postscript/.

Back to topic list...