ssh fails to execute remote command when run from cron bash script - works from CLI
Solution 1:
Try adding -t -t
to your SSH connection options. This will; force a pseudo-terminal to be allocated.
Multiple
-t
options force tty allocation, even ifssh
has no local tty.
Solution 2:
You should debug your script further. Two things to try:
Rwrite it to do file output directly instead of through output redirection. That is, in the script do this:
$SSH_BINARY -i $SSH_KEY $SSH_USER@$REMOTE_HOST "ls $REMOTE_DIRECTORY" > savefile
Run your script with bash -x inside cron and save result. Change first line of script to #!/bin/bash -x
and try something like
34 * * * * /root/path/to/my/script/get_logs.sh > script_debug 2>&1
that should help you narrow down exactly what your script is doing. I suspect redirection of output in cron is not working correctly in this case.
Also, dumb question: have you verified that root can write to /root/path/to/last_run_output
? Have you verified that you don't have noclobber
set somehow when the script runs in cron?
EDIT: further troubleshooting ideas based on comments from OP.
So none of the above ideas seem to be working. What about saving the fielist in a file on the remote machine and scp'ing it back? That would eliminate any weird quoting or input redirection problems.