The script below works fine when running from command line. I also put in a Cron Job, and I know the Cron Job is running because I see entries in the sftpFileLog.txt.
But when running under Cron Job, the only thing I see in the sftpLog.txt is the line at the top that draws the line and puts the current date/time. Also the two files I'm expecting don't show up on the SFTP server when running under Cron Job (but they do when running Bash Script directly.
I was trying to combine a "here doc" and redirection to a file per several other posts I found.
Is it possible I have security issues or a code problem? I'm an experienced IT guy, but new to bash scripts and Linux. (This is Ubuntu 20.04).
#!/bin/bash currentDate=`date` echo "------- $currentDate ------------------------------------------------------------" >>/home/nealwalters/bin/sftpFileLog.txt echo "------- $currentDate ------------------------------------------------------------" >>/home/nealwalters/bin/sftpLog.txt for filename in /home/nealwalters/downloads/*.csv; do currentDate=`date` echo "Loop: Filename=$filename" echo "$currentDate SFTPUser3 Filename=$filename" >> /home/nealwalters/bin/sftpFileLog.txt ( sftp -i /home/nealwalters/keys/SFTPUser1_Private.pem [email protected] << EOF cd User3ToMyCompany put $filename exit EOF ) >> /home/nealwalters/bin/sftpLog.txt echo "----------------------------------------------------------------" done Example from sftpLog.txt file (lines highlighted in yellow is expected result as seen when I run script from terminal, and lines below that are each minute thereafter when run from the Cron Job).
----- Part 2 -------
I can't believe that adding the email gave the error, and that it wouldn't be in one of two other places: 1) the syslog 2) why my redirect didn't write it the file I gave.
I referenced this post to get info on how to setup and see the email.
Question 1 - Is my redirect mixed with here doc totally wrong then? Question 2 - Why would I get "host key verification failed" when I run under Cron but no when I run myself? I will try user=nealwalters instead of user=root to see if that makes a difference.
----- Part 3 -------
Changed from "root" to "nealwalters" seemed to fix it.
I'm thinking that I didn't route "stderr" - needed something like this: out 2> sftpLog.txt.
Doesn't root have all access? But I know private keys can be weird, and only supposed to have access to one user.


cat /var/mail/username