2

I'm trying to run multiple rsync commands from a bash script but only the first one fires. I've tried linking the commands with && and ; but neither work.

#! /bin/bash
rsync -ahPr --delete-excluded --exclude='.DS_Store' --exclude=Caches/ --ignore-errors --stats --log-file=$LOGFILE --files-from=$FILESFROM ~/$SOURCE /Volumes/$DEST &&
rsync -avh --progress ~/.profile /Volumes/$DEST/_profile/profile &&
rsync -avh --progress ~/.gitconfig /Volumes/$DEST/_profile/gitconfig &&
rsync -avh --progress ~/.ssh/ /Volumes/$DEST/_profile/ssh/ &&
rsync -avh --progress ~/.vim/ /Volumes/$DEST/_profile/vim/

I'm doing the last 4 commands as seperate ones because I'm renaming the files/directories on the fly—not sure if there's a better way to do this?

I'm executing the script with bash -x myscript.sh to see if it comes up with any errors, but no.

1
  • 2
    Remove all && from your script.
    – Cyrus
    Sep 22, 2017 at 4:59

1 Answer 1

6

Running

rsync && rsync

will start the second rsync after the first one completes. You want

rsync&

Explanation:

a && b

a runs, b runs next only if a competes successfuly

a || b

a runs, b runs next only if a completes UNsuccessfully

a; b

a runs, b runs next no matter the success

a& b

a runs, gets sent into the background. This is equivalent to running a, typing ctrl-Z and the running bg

So if you want your programs to run at the same time, replace the &&s with single &

5
  • Thanks for the tips on the spacing needed. Regardless it's still not working. If I comment out the first larger rsync command, the remaining 4 work fine. Do I have a timing issue?
    – sansSpoon
    Sep 22, 2017 at 4:29
  • OK, I've swapped the longer command from first to last. They now all fire correctly. I can only presume that there is a timing issue, but would appreciate it if someone could confirm this?
    – sansSpoon
    Sep 22, 2017 at 4:38
  • Not a timing issue. Since the last four commands don't execute if the long one is run first, but do if the long one is running last, and the next command in the chain only execute if the previous one succeeds (due to the double-'&'s), the issue is the long command exits with an error. (@Facebook - this? newbsauce here) Sep 22, 2017 at 4:54
  • The first was indeed having an error. I cleared the error and changed everything to a; b. It works now. Thanks @Peter Berbec
    – sansSpoon
    Sep 22, 2017 at 5:26
  • Glad to help! I've gotten help here dozens of times, but never created an account. I feel warm and fuzzy! Sep 22, 2017 at 5:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .