Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have a script, which has an sftp command:

echo "Starting to sftp..."
sftp << 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 ( redirect from cron )

sftp -b batch_file.txt >> 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/ > /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.

share|improve this question
Why not use tar/cpio over ssh to transfer? – Ignacio Vazquez-Abrams Mar 28 '12 at 2:58
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 – Michael Mrozek Mar 28 '12 at 6:38
I just ran a quick check, and sftp << 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? – Shadur Mar 28 '12 at 7:15
On a related note, how precisely are you redirecting it in cron? Can you show us the exact crontab entry? – Shadur Mar 28 '12 at 7:18
Red Hat Enterprise Linux AS release 3 (Taroon Update 2). How can I check the ssh version? – lamwaiman1988 Mar 28 '12 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/ > /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.

share|improve this answer
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. – lamwaiman1988 Mar 29 '12 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. – lamwaiman1988 Mar 29 '12 at 3:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.