Monday, July 25, 2022

[SOLVED] How to execute bash script after rsync

Issue

When I deploy on my dev server I use rsync. But after rsync I need to execute a .sh file for "after" deploy operations like clear cache...

Usually I do this via SSH, but if I deploy very often it's boring write:

  • ssh ...
  • write password
  • cd /var/www/myapp/web
  • ./after_deploy.sh

There is a way to do this quickly? This is my rsync.sh:

#!/bin/bash
host=""
directory="/var/www/myapp/web"
password=""
usage(){
        echo "Cant do rsync";
        echo "Using:";
        echo "   $0 direct";
        echo "Or:";
        echo "   $0 dry";
}
echo "Host: $host";
echo "Directory: $directory"
if [ $# -eq 1 ]; then
        if [ "$1" == "dry" ]; then
                echo "DRY-RUN mode";
                rsync -CvzrltD --force --delete --exclude-from="app/config/rsync_exclude.txt" -e "sshpass -p '$password' ssh -p22" ./ $host:$directory --dry-run
        elif [ "$1" == "direct" ]; then
                echo "Normal mode";
                rsync -CvzrltD --force --delete --exclude-from="app/config/rsync_exclude.txt" -e "sshpass -p '$password' ssh -p22" ./ $host:$directory
        else
                usage;
        fi;
else
        usage;
fi

Solution

You can add a command after the rsync command to execute it instead of starting a shell.

Add the following after the rsync command :

sshpass -p "$password" ssh $host "cd $dir && ./after_deploy.sh"


Answered By - paul
Answer Checked By - Marie Seifert (WPSolving Admin)