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


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Unable to sftp with cron (once more time...)
# 1  
Old 10-08-2008
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 Smilie). Anyway, here is my question.

I wrote a sftp.sh file that runs perfect from command line. It uses scp for getting a file from a remote server. I use a key pair for the authentification, passphrase and ssh-agent included, as I learnt from the forum.

Then I added a task in the user crontab. The crontab works fine, as I added a trivial line in the sftp.sh script and it is executed as expected. But the scp line does not work.

The sftp.sh script scheme is:

#!/usr/bin/ksh
/usr/bin/scp root@remote.server:/path/file1 file2
exit 0

In case I use sftp instead of scp:

#!/usr/bin/ksh
sftp -b /dev/stdin root@remote.server <<EOF
get file
quit
EOF
exit 0

I found some comments on the environment of cron, but I was not able to solve the problem with those references...

I run it on Red Hat Enterprise 5. If I find any solution I'll post it, but I'm completely run out of ideas (only few weeks linux user, so I don't have many of them, in any case!)

Any help???? Thanks!!!
# 2  
Old 10-08-2008
By user crontab do you mean root's crontab?
# 3  
Old 10-08-2008
I mean that I added the task via "crontab -e", from my user profile...
thanks
# 4  
Old 10-08-2008
Well, at the end I was able to make it work! Smilie))))

Once I could get the errors (access denied!) I realised that it was actually a problem with the environmental variables so after trying again with the older posts, I had to go through the export and printenv issues.

In my environment I found two variables related to the ssh agent (SSH_AUTH_SOCK and SSH_AGENT_PID) so I added them to the script:

#!/usr/bin/ksh
export SSH_AUTH_SOCK=... (copied after the command printenv)
export SSH_AGENT_PID=... (idem)
/sftppath/scp root@remoteserver:/remotepath/remotefile /localpath/localfile
exit 0

And it worked!!! Smilie Not easy for me as a newbie, however... I post the solution here in case it could serve for anyone.

bye!!

PD: I guess I'll have problems with the agent id again, when I login next time, anyway...
# 5  
Old 12-10-2008
SCP not working from cron, here is the key answer

Hi,

CASE : If you are using scp in your script and is working fine. Then, if running the script from crontab is not working for you!!!
ex:In my case it was mysql backup script...

Solution: Include the PATH variable in to your shell script, which you are putting in to crontab.
ie: export PATH=
Put the path variable as in your environmental PATH variable which you get when you say env on your command prompt.

Logic behind the soultion : The cron job for your script (which includes scp) might not getting the path to your rsa keys..., so, by exporting the PATH in to your script will/should solve it.

Good luck.
# 6  
Old 12-10-2008
ref my prev post
# 7  
Old 05-06-2009
Thanks Kronos, that post helped me out bigtime.

Thanks Kronos, that post helped me out bigtime.

Quote:
Originally Posted by Kronos
Well, at the end I was able to make it work! Smilie))))

Once I could get the errors (access denied!) I realised that it was actually a problem with the environmental variables so after trying again with the older posts, I had to go through the export and printenv issues.

In my environment I found two variables related to the ssh agent (SSH_AUTH_SOCK and SSH_AGENT_PID) so I added them to the script:

#!/usr/bin/ksh
export SSH_AUTH_SOCK=... (copied after the command printenv)
export SSH_AGENT_PID=... (idem)
/sftppath/scp root@remoteserver:/remotepath/remotefile /localpath/localfile
exit 0

And it worked!!! Smilie Not easy for me as a newbie, however... I post the solution here in case it could serve for anyone.

bye!!

PD: I guess I'll have problems with the agent id again, when I login next time, anyway...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to do SFTP using expect

Hi, I am trying to sftp using expect, but not getting through as it prompt for password is coming Following is the code #/usr/bin/expect > output.log sftp medcdr@10.130.254.50 expect "password:" send "Med@Cdr12\n" expect "sftp>" send "put ZTE_*201505*\r" expect "sftp>" send "bye\r"... (7 Replies)
Discussion started by: siramitsharma
7 Replies

2. Shell Programming and Scripting

Cron job - Need to run Cron every quarter at particular time

Hi, 1) If some job supposed to run on 1st of every month at 7 AM In cron job when we have a blackout on the 1st ( i.e when 1st falls on a sunday ) how can we make the job run the next business day? 2) How can we run a job on 25th of every quarter 7 AM(jan,apr,jul,oct) And if 25th... (5 Replies)
Discussion started by: System Admin 77
5 Replies

3. Solaris

System time and Cron time stamp not matching

On Solaris 10 server the system date won't match with the timestamp on files created by a cron jobs, Please help here is what i get when i check for system date infodba-ie10ux014:/tcpdv1_ie10/tcadmin/bin\n\r-> date Tue Apr 24 15:27:43 GMT 2012at same time i executed a cron job, and checked... (4 Replies)
Discussion started by: karghum
4 Replies

4. UNIX for Advanced & Expert Users

SFTP Not Working With CRON

Hello - I have a production stream that is scheduled with cron to run each Monday morning. The jobs in the stream perform tasks including FTP get, load to a DB table, and report processing. About a month ago I was directed to begin using sftp in these jobs and since then the jobs... (12 Replies)
Discussion started by: PatrickPurfield
12 Replies

5. Emergency UNIX and Linux Support

Unable to connect using SFTP

I shall explain the situation that I am facing to the best extent possible. I require some help, as this situation is an urgent one. I am trying to automate sending data from one AIX machine to another. A script runs that tries to push data received from an upstream application to another AIX... (7 Replies)
Discussion started by: ggayathri
7 Replies

6. Shell Programming and Scripting

unable to schedule in cron

the follwing script is running fine , when run from the ksh shell...it runs properly i.e. waits for a file "test.flag" and sends mail accordingly. But when I schedule it in the crontab ...it does not run at the specified time (it never runs). I am using ksh ####shell script starts count=1... (4 Replies)
Discussion started by: ace@123
4 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. UNIX for Dummies Questions & Answers

Unable to scp/sftp between two servers

I have four servers that for all intents and purposes are the same (I have the same profile on all four), North, South, Brooklyn & Queens. I have a script that scp's a file from Queens to brooklyn, and it runs just fine. I tried to replicate the script on South, to transfer a file to North, and... (1 Reply)
Discussion started by: DeCoTwc
1 Replies

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

10. 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
Login or Register to Ask a Question