SFTP fails from crontab but works from terminal


 
Thread Tools Search this Thread
Top Forums Programming SFTP fails from crontab but works from terminal
# 1  
Old 08-16-2012
SFTP fails from crontab but works from terminal

Dear community,
I'm driving crazy with a strange issue.

I have a simple script to transfer a file to a remote system:
Code:
#!/bin/bash
echo "put /tmp/server.log" > /tmp/server1_transfer.sftp
sftp -b /tmp/server1_transfer.sftp user@10.99.1.2:

Between client and server there is a SSH KEY, so if I run the script from root terminal, the file is correctly transferred.

Now, I edit the root crontab to automatically execute this script, but the trasfer fails!! Smilie

Seems the SSH KEY doesn't work from root cron, because every time the script runs, the system send me the following mail:
Code:
Received: (from root@localhost)
        by xyz (8.13.8/8.13.8/Submit) id q7GGv1qv022647;
        Thu, 16 Aug 2012 18:57:01 +0200
Date: Thu, 16 Aug 2012 18:57:01 +0200
Message-Id: <201208161657.q7GGv1qv022647@xyz>
From: root@xyz (Cron Daemon)
To: root@xyz
Subject: Cron <root@xyz1> /tmp/test.sh
Content-Type: text/plain; charset=ANSI_X3.4-1968
Auto-Submitted: auto-generated
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
X-Cron-Env: <USER=root>

Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey).

Please, could someone explain me why from terminal works and from crontab doesn't work? Smilie

Many thanks
Lucas
# 2  
Old 08-16-2012
Make sure it's using the correct key. Point it to it with -i if necessary.
# 3  
Old 08-16-2012
The copy command fails because cron is not a regular shell. I'm on my phone but the SCP/SSH man pages explain how to provide a config directory/key file option to the command
# 4  
Old 08-16-2012
Thanks both for tips, but I believe I cannot provide to SFTP the SSHKEY path. This is what I seen on the SFTP manual.

Code:
     sftp [-1Cv] [-B buffer_size] [-b batchfile] [-F ssh_config] [-o ssh_option] [-P sftp_server_path]
          [-R num_requests] [-S program] [-s subsystem | sftp_server] host
     sftp [[user@]host[:file [file]]]
     sftp [[user@]host[:dir[/]]]
     sftp -b batchfile [user@]host

# 5  
Old 08-16-2012
What's your system?
# 6  
Old 08-16-2012
Quote:
Originally Posted by Corona688
What's your system?
# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 5.7 (Tikanga)
# 7  
Old 08-16-2012
You have openssh then, which supports -i. See man sftp.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string works on command-line but fails when run from shell script

I wish to replace "\\n" with a single white space. The below does the job on command-line: $ echo '/fin/app/scripts\\n/fin/app/01/sql' | sed -e 's#\\\\n# #g'; /fin/app/scripts /fin/app/01/sql However, when i have the same code to a shell script it is not able to get me the same output:... (8 Replies)
Discussion started by: mohtashims
8 Replies

2. Shell Programming and Scripting

Find command works on Linux but fails on Solaris.

Hi, I am looking for a generic find command that works on both Linux and Solaris. I have the below command that works fine on Linux but fails on solaris.find /web/config -type f '(' -name '*.txt' -or -name '*.xml' -name '*.pro' ')' Fails on SunOS mysolaris 5.10 Generic_150400-61 sun4v sparc... (1 Reply)
Discussion started by: mohtashims
1 Replies

3. Shell Programming and Scripting

Need to know why crontab -e fails

more cron.txt 1 * * * * /u/ways.sh 2>&1 >/dev/null bash-4.1$ export EDITOR=vi bash-4.1$ crontab -e <cron.txt Vim: Warning: Input is not from a terminal Vim: Error reading input, exiting... Vim: Finished. The crontab file was not changed. bash-4.1$ echo $? 1 bash-4.1$ uname -a SunOS... (3 Replies)
Discussion started by: mohtashims
3 Replies

4. Shell Programming and Scripting

sed works on Linux but fails on Solaris

Hi, On Linux i get the desired ouput: echo "<value>WEB_USER</value>" | sed 's/\(<value>\|<\/value>\)//g'Output: Executing the same command on Solaris: echo "<value>WEB_USER</value>" | sed 's/\(<value>\|<\/value>\)//g'Output: I need to get the desired output on Solaris i.e. WEB_USER and... (4 Replies)
Discussion started by: mohtashims
4 Replies

5. Shell Programming and Scripting

awk works on Linux but fails on Solaris

On linux i have the below command working fine. awk '/<app-deploy>/{A=1;++i} /<\/app-deploy>/{print >> "found"i".tmp";A=0} A{;print >> "found"i".tmp"}' deploy.xml But the same is failing on Solaris Output: awk: syntax error near line 1 awk: bailing out near line 1 uname -a SunOS mymac 5.10... (5 Replies)
Discussion started by: mohtashims
5 Replies

6. Shell Programming and Scripting

Grep works on Linux but fails on Solaris

Hi, On linux i have the below command working fine. grep -o '<name>.*</name>' deploy.tmp | sed 's/\(<name>\|<\/name>\)//g' deploy.tmp But the same is failing on Solaris uname -a SunOS mymac 5.10 Generic_150400-23 sun4v sparc sun4v Can you tell me how can i get it work on Solaris ?... (6 Replies)
Discussion started by: mohtashims
6 Replies

7. Shell Programming and Scripting

SED command works in terminal, but not Applescript

The following command works perfectly in Terminal, but not in Applescript. (Returns "unknown token" error for square brackets.) (new to site. sorry.) I have an Applescript that is designed to find and remove any square-bracketed text, including the square brackets. I ran the following code from... (1 Reply)
Discussion started by: Phillip Acosta
1 Replies

8. Shell Programming and Scripting

Help wth Net::FTP in script sometimes fails but wsftp always works

Hi, I have a script that transfers files from a Solaris server to target unix servers. The script uses Net::FTP->put to write the files, which can be any number of files, not always the same. This script works flawlessly to many servers. For one particular instance of the script,... (3 Replies)
Discussion started by: csgonan
3 Replies

9. Shell Programming and Scripting

how it works ? sftp

Hi, I am curious about this script , how it is running ..? #!/bin/sh echo "OK, starting now..." ftp remotehost <<EOF When I run , it is asking OK, starting now... Password:Name (remotehost): SHould I enter only password ? and explain me how it works.. thanks in advance.. (3 Replies)
Discussion started by: hegdeshashi
3 Replies

10. Shell Programming and Scripting

Cron job fails, but works fine from command line

I have a very basic script that essentially sends a log file, via FTP, to a backup server. My cron entry to run this every night is: 55 23 * * * /usr/bin/archive_logs The script runs perfectly when executed manually, and actually worked via cron for about three weeks. However, it mysteriously... (3 Replies)
Discussion started by: cdunavent
3 Replies
Login or Register to Ask a Question