I have a script a.sh, which has an sftp command:

echo "Starting to sftp..."
sftp [email protected] << END_SCRIPT
mput *.csv.gz

echo "Sftp successfully."

If I run it interactively, the log output from sftp is shown. However, if I set it in a crontab and redirect the stdout to a file using >, there will be only the output from the two echo statements. How can I get the log from the sftp command?


I tried the following forms and it didn't work too.

sftp -b batch_file.txt [email protected] ( redirect from cron )

sftp -b batch_file.txt [email protected] >> some_file.log ( redirect from cron + redirect from this line )

I think the problem should be related to the nature of sftp that it is supposed to be run interactively, as stated in the man page. But I cannot find the solution.

My distribution : Red Hat Enterprise Linux AS release 3 (Taroon Update 2)

the crontab entry:

52 14 * * * sh /home/xxx/testing/xxx.sh > /home/xxx/testing/xxx_`date +\%Y\%m\%d`.log

And the log is created with a time matching the crontab entry.

ssh version:

OpenSSH_3.6.1p2, SSH protocols 1.5/2.0, OpenSSL 0x0090701f


I found that if I redirect the output of sftp in the script file to an text file, the message from sftp is logged. Then I remove the log file and scheduled it with cron. After the cron execution, the log file is generated but have no message from sftp. Instead, it only contain log from other statement inside the script file.

If I redirect the output of sftp in script file and run it interactively, the log are perfectly fine. However if I scheduled it with cron ( no redirect in the crontab entry, just simply schedule it ), the log from sftp is gone too.

  • Why not use tar/cpio over ssh to transfer? Commented Mar 28, 2012 at 2:58
  • 1
    This got flagged for migration to SO, but it's far more on-topic here; this is a question about sftp or cron, depending on where the problem is Commented Mar 28, 2012 at 6:38
  • I just ran a quick check, and sftp [email protected] << EOF > sftpout.txt gave me most of sftp's output where it was supposed to go. What version of ssh and what distribution are you using? Commented Mar 28, 2012 at 7:15
  • On a related note, how precisely are you redirecting it in cron? Can you show us the exact crontab entry? Commented Mar 28, 2012 at 7:18
  • Red Hat Enterprise Linux AS release 3 (Taroon Update 2). How can I check the ssh version? Commented Mar 28, 2012 at 7:18

1 Answer 1


From the looks of it, output redirection is occurring, but you probably want to include stderr, too. Try this in your crontab:

52 14 * * * sh /home/xxx/testing/xxx.sh > /home/xxx/testing/xxx_`date +\%Y\%m\%d`.log 2>&1

Note the 2>&1 at the end. It means: "redirect file descriptor 2, stderr, to whatever file descriptor 1, stdout, is." Because you already redirected stdout to the file, stderr will end up in there as well.

  • No it doesn't work and not making sense too. Since I can redirect the std output of sftp when I run the script manually, it is clear that the output isn't in std err channel. And I tried it too, it didn't work. Commented Mar 29, 2012 at 1:39
  • I found out the reason. I must admit you are right, though the 2>&1 didn't give me much clue when I first tried it. I will post the solution as an asnwer. Commented Mar 29, 2012 at 3:55

You must log in to answer this question.

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