SFTP Not Working With CRON


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users SFTP Not Working With CRON
# 8  
Old 01-19-2011
When people say "works in shell, not in cron", 90% of the time it's the PATH, because cron's default path is way more minimal than the shell's default path. As I suggested before, either set a better PATH, or call programs by their absolute paths.

On my system sftp's absolute path is /usr/bin/sftp, I can't speak for yours; and /usr/ things almost surely aren't in cron's default PATH. Things like mailx and date might also need absolute paths. cat hopefully shouldn't on a sane system.

You might also try editing your cron line like
Code:
32 11 * * 1 /home/prodid/data/LMC/accept/WeeklyLabor/sql_loader/sftp_testing/sftp_testing.job abusmgt /appl/oracle1/product/10.2.0.4 /appl/oracle1 >> /path/to/file.log 2>> /path/to/file.err

so that when things go wrong, it dumps the error messages for you into these logfiles instead of hyperspace.
# 9  
Old 01-19-2011
What Operating System and version are you running?

Which user owns the cron? Just need to know whether is is not root.

Does "crontab -l" retrieve the lines from the crontab as you expect?

Quote:
ORACLE_SID=$1
export ORACLE_SID
ORACLE_HOME=$2
export ORACLE_HOME
ORACLE_BASE=$3
export ORACLE_BASE
You need to "export" all three variables. When run from your interactive account maybe they are already exported.

Any unredirected error messages from when the script is run from cron will be in unix mail for the account owning the cron.
# 10  
Old 01-19-2011
Hello All -

It was a PATH issue. Adding the following at the beginning of the script containing the sftp command corrected the problem:

PATH=/app/share/bin:$PATH
export PATH

Thanks to all responders!

Sincerly,
Patrick
# 11  
Old 01-25-2011
Use SFTP.exp ...

I had a similar problem, and I have resolved with this script:

#!/usr/local/bin/expect -f #<---insert here your expect program location
# procedure to attempt connecting; result 0 if OK, 1 elsewhere
proc connect {passw} {
expect {
-re ".*Are.*.*yes.*no.*" {
send "yes\r"
exp_continue
}
-re ".*PASSWORD.*" {
send "$passw\r"
expect {
"sftp*" {
return 0
}
}
}
-re ".*password.*" {
send "$passw\r"
expect {
"sftp*" {
return 0
}
}
}
-re ".*Password.*" {
send "$passw\r"
expect {
"sftp*" {
return 0
}
}
}
}
# timed out
return 1
}
# read the input parameters
set user [lindex $argv 0]
set passw [lindex $argv 1]
set host [lindex $argv 2]
set remotepath [lindex $argv 3]
set localpath [lindex $argv 4]
set file1 [lindex $argv 5]
set file2 [lindex $argv 6]
# check if all were provided
if { $user == "" || $passw == "" || $host == "" || $remotepath == "" || $localpath == "" || $file1 == "" } {
puts "Usage: <user> <passw> <host> <remote path> <local path> <file to send> [<file to send>]\n"
exit 1
}

# sftp to specified host and send the files
spawn /usr/local/bin/sftp $user@$host
set rez [connect $passw]
if { $rez == 0 } {
send "lcd $localpath\r"
send "cd $remotepath\r"
set timeout -1
send "put $file1\r"
if { $file2 != "" } {
send "put $file2\r"
}
send "quit\r"
expect eof
exit 0
}
puts "\nError connecting to server: $host, user: $user and password: $passw!\n"
exit 2


it copies this code in file SFTP.exp and then call from your shell in this way:
SFTPLOG=`${EXEPATH}SFTP_FOR_C1.exp ${USER} ${PASS} ${HOST} ${RPATH} ${LPATH} '*'`
and verify ${SFTPNUM} with number of records in retrieved file:
SFTPNUM=`echo "${SFTPLOG}" | grep "100%" | wc -l | awk '{print $1}'`
if [ ${SFTPNUM} -ne ${DFILE} ]
then
echo "WARNING: number of records......."
exit 1
fi
# 12  
Old 01-25-2011
Your problem wasn't similar at all, and your "solution" is completely unrelated. Whether you use expect or not it still would not have been in the PATH.

Furthermore: sftp is designed to not let you use plaintext passwords. So does any sane authentication system, such as su and sudo. It's a security feature to prevent the plaintext password being stored and transmitted with insecure methods, and to slow down brute-forcing. That you had to bludgeon sftp into doing what you wanted with the external "expect" language is a subtle hint -- in mile-high, flashing neon letters -- that you're not supposed to do that.

There's much better, passwordless, secure, reliable authentication methods that don't require an external tool hack, and you'll find them in 30 seconds if you google "passwordless ssh". The poster of this thread is in fact using them already.
# 13  
Old 01-25-2011
dear signor Corona,
I could have understood badly also, but my solution it was alone to help...
I don't need his useless lesson
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Mget with SFTP is not working

hi Team, I am connecting from one (A) linux server to another(C)/any linux server by sftp on A linux server: sftp userid@C password: mget is Not working fine I am using mget to pull the files. it shows mget as invalid command. But from (B) Linux server to (C) /to Any server Linux... (15 Replies)
Discussion started by: johnsnow
15 Replies

2. Shell Programming and Scripting

sftp autologin is working but ...

Dears, I am new to linux scripting and I was look for a way to auto login to a server using sftp to download a file. I found one and it is working fine. But i don't know the meaning of <<EOF in the code. Any one explain it to me: #!/bin/sh HOST=yourservername USER=yourusername ... (1 Reply)
Discussion started by: torabi
1 Replies

3. Shell Programming and Scripting

Script not working in cron but working fine manually

Help. My script is working fine when executed manually but the cron seems not to catch up the command when registered. The script is as follow: #!/bin/sh for file in file_1.txt file_2.txt file_3.txt do awk '{ print "0" }' $file > tmp.tmp mv tmp.tmp $file done And the cron... (2 Replies)
Discussion started by: jasperux
2 Replies

4. Shell Programming and Scripting

Script is not working from cron while working manually

Hello, I am facing a very strange problem when I run my script manuallu ./Fetchcode which is using to connect with MKS integrity from linux end it workks fine but when I run it from cron it doesn't work.Can someone help me 1) How could I check my script when it is running from cron like... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

5. UNIX for Dummies Questions & Answers

Problem automating sFTP transfer using script in cron

Hi Newbie here I am having problems with automating sFTP transfers. Just to save time - SCP is not an option as sFTP is stipulated by controllers of far end server. Ineed to automate sFTP transfer of a single file, once a day to a remote server to which i have no control over. I am using:... (6 Replies)
Discussion started by: robbien
6 Replies

6. UNIX for Dummies Questions & Answers

Unable to sftp with cron (once more time...)

Hi, I have been searching for older posts and I've found many entries with identical problems, but I was not able to find the solution (or when I thought I found it, my trial didn't worked :(). Anyway, here is my question. I wrote a sftp.sh file that runs perfect from command line. It uses scp... (6 Replies)
Discussion started by: Kronos
6 Replies

7. UNIX for Dummies Questions & Answers

sftp in cron

Hi, I am running simple script to automate sftp transfer to remote box. I have setup public/private keys to have sftp connect automatically and have test script that list remote directory: #!/bin/ksh echo "OK, starting now..." sftp userid@host <<EOI cd dir ls -lt bye EOI... (2 Replies)
Discussion started by: r1omen
2 Replies

8. Solaris

SFTP not exiting when run from cron

I am using a script to transfer a file from a unix host to another unix host. The code snippet for sftp in the script is as below. sftp -oIdentityFile=$ID_FILE_NAME -oNumberOfPasswordPrompts=0 $REMOTE_USERID@$REMOTE_HOST <<EOF cd incoming put $REPORT_FILE... (2 Replies)
Discussion started by: msabhilash
2 Replies

9. Shell Programming and Scripting

SFTP not working in cron

Hi, I have a simple script that is trying to put a file that resides on a local machine to a remote machine. It runs fine manually but does not complete when scheduling to run in cron. Here is what the script looks like. Any idea what I am doing wrong here? #!/bin/ksh cd /path sftp... (4 Replies)
Discussion started by: ewilson0265
4 Replies

10. Solaris

SFTP errorcode 1 when run on cron but runs manually

I am trying to run a sript on cron to SFTP data to a company. Private and public keys are set up. When I run this manully it works fine, however it was failing when run on cron. I have narrowed down the problem - it fails at the code that says if the error code is 0 then continue . . . I... (2 Replies)
Discussion started by: Heidi.Ebbs
2 Replies
Login or Register to Ask a Question