rsync or ssh failing from cron or background script

Came across this again and thought I'd write it up. Basically we had an rsync/ssh job that was being called by tool. The command ran just fine with all manual tests and experiments, but when called unattended by another tool, the following error resulted:

usage: ssh [-1246AaCfgkMNnqsTtVvXxY] [-b bind_address] [-c cipher_spec]
           [-D [bind_address:]port] [-e escape_char] [-F configfile]
           [-i identity_file] [-L [bind_address:]port:host:hostport]
           [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
           [-R [bind_address:]port:host:hostport] [-S ctl_path]
           [-w tunnel:tunnel] [user@]hostname [command]
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: error in rsync protocol data stream (code 12) at io.c(600) [receiver=3.0.6]

The solution was to add the username to the rsync/ssh command as in rsync -ravz user@server.com /foo.

Very odd behavior. I suspect that something is missing in the environment when running without a TTY attached.

\@matthias