0

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).

enter image description here

----- 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.

enter image description here

----- 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.

4
  • 1
    Please provide the output for cat /var/mail/username Commented May 8, 2020 at 19:52
  • @binarysta "cat /var/mail/nealwalters gave "No such file or directory". I have seen the MTA errors in the syslog ("No MTA installed, discarding output"). Would that apply though if output was redirected? Does that only apply to errors or successful runs as well. Commented May 8, 2020 at 20:21
  • Could it be rather than writing the error to syslog, it tries to email the error, and I don't have email setup? That would seem like a round about way of logging errors. Commented May 8, 2020 at 20:26
  • I installed email and updated with Part 2 and Part 3. Commented May 8, 2020 at 21:21

2 Answers 2

1

Just a wild guess but wouldn't this also work? scp -i /home/nealwalters/keys/SFTPUser1_Private.pem /home/nealwalters/downloads/*.csv [email protected]:User3ToMyCompany

You could also create ~/.ssh/config with the contents

Host company Hostname 192.168.1.179 User STFPUser3 IdentityFile /home/nealwalters/keys/SFTPUser1_Private.pem 

And then you can do it with just:

scp /home/nealwalters/downloads/*.csv company:User3ToMyCompany

The reason you get host key verification failed is that the user executing the script does not have the hostkey for that host. You seem to run the cronjob as root. You can also create the cronjob with your own user with crontab -e.

4
  • I'm not sure, the SFTP server is running on a Windows machine with a server called CompleteFTP. So it supports SSH, does that mean it will support the SCP command? I'm actually making a course for OpenSSH and related topics. But Windows is my main OS normally. Commented May 8, 2020 at 21:07
  • Also, I did try with my user and that solved, see Part3 update to question. I was probably testing that about the same time you were answering. Commented May 8, 2020 at 21:09
  • I tried the you scp command. Gives three lines of output: 1) Complete FTP server [I think that was the welcome message from the server I'm running], 2) exec request failed on channel 0, 3) lost connection. And the files was not copied. Commented May 8, 2020 at 21:13
  • See, nowhere in your comments does it say you're target is a windows server which only supports sftp... Makes it very hard to understand your problems. Anyway, my comments about crontab still stands. Commented May 9, 2020 at 17:11
1

I referenced this post to get info on how to setup and see the email.

Had to install an email MTA (Mail Transfer Agent) as stderr and stdout are apparently sent to email.

aptitude install postfix 

Use a command like this to see last few lines of the file (or open in editor)

tail -f /var/mail/<your_username> 

Then I saw the security error, and had to switch from "root" to "" for the user in the cron job file.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.