I have resolved the above error by setting timeout time.
now the script is running fine but when I tried to run through it is showing error and state file not found
script mentioned below
#!/usr/bin/expect
set DATE [exec date -d "1 day ago" +%Y-%m-%d]
spawn sftp AUT#XX.XX.XX.XX
#spawn echo $DATE
expect "password:"
send "AT123\n"
expect "sftp>"
send "cd /home/ \r"
expect "sftp>"
send "LOGS_$DATE.tar.gz\r"
set timeout 2000
expect "sftp>"
send "quit \r
getting below error when script run through cron
# log
+ log
spawn sftp AUT@xx.xx.xx.xx
Connecting to xx.xx.xx.xx...
AUT@xx.xx.xx.xx's password:
sftp> cd /home
sftp> put LOGS_2018-08-05.tar.gz
stat LOGS_2018-08-05.tar.gz: No such file or directory
Personally I think you are making the operation more complicated than it needs to be.
Are you able to use an SSH key pair to make the connection? This would get rid of the need to send passwords.
Are you only sending the one file? The script you have shown in previous posts could have been stripped down to show the problems you are experiencing. If it has not, and you are only sending one file, you could use SCP instead of SFTP, provided you can make use of an SSH key pair. Your script could then be something like:
#!/bin/bash
remuser=xxx
remhost=yyy
remdir=zzz
logfile=$(date -d "1 day ago" +"LOGS_%Y_%m_%d.tar.gz")
if [[ -e ${logfile} ]]
then scp ${logfile} ${remuser}@${remhost}:${remdir}
else echo "Cannot find log file ${logfile}"
fi
My current problem is that when I am trying to run my script by scheduling it in cron it did not work.
however when the same script I run manually it works.
Hi Andrew,
yes the path is same when I run manually or when it runs from cron.
below error I get when script run from cron
# log
+ log
spawn sftp AUT@xx.xx.xx.xx
Connecting to xx.xx.xx.xx...
AUT@xx.xx.xx.xx's password:
sftp> cd /home
sftp> put LOGS_2018-08-08.tar.gz
stat LOGS_2018-08-08.tar.gz: No such file or directory