Operating System - HP-UX
1838653 Members
4039 Online
110128 Solutions
New Discussion

Re: Expect script on crontab

 
SOLVED
Go to solution
Dewa Negara_4
Regular Advisor

Expect script on crontab

Hi All,

I have an expect script called sftp to transfer using SFTP below :

[root@prod:/root]
# more sftp
#!/usr/local/bin/expect

# Initialisation
set authFile "/home/ap1030/transfer/.password"

# Check the authorisation file exists
if {![file exists $authFile]} { ;# Does file exist
send_user "$authFile does not exist; aborting\n"
exit 1
}
set fileFD [open $authFile r] ;# Open the auth file
gets $fileFD authLine ;# Read in 1 line to authLine
close $fileFD

# Transfer file(s)
spawn /opt/openssh/bin/sftp -b /home/ap1030/transfer/batchFile importve@www.importve.com
expect "password:"
send "$authLine\n";
interact

When I run this script manually from command line, it was running fine. But when I put on crontab, it was NOT working. I believe this is related to "interact" command on the last line. How can I make the script running from crontab? Pls help. High score will be give.

Below is my crontab entry and the log file. .

05 15 * * * /usr/local/bin/expect -f /home/ap1030/transfer/sftp > /tmp/sftp 2>1&

[importve@prod:/home/ap1030/transfer]
$ more /tmp/sftp
spawn /opt/openssh/bin/sftp -b /home/ap1030/transfer/batchFile importve@www.importve.com
Connecting to www.importve.com...
importve@www.importve.com's password:

Thanks and Best Regards,
Dewa
Santos
5 REPLIES 5
Fred Ruffet
Honored Contributor
Solution

Re: Expect script on crontab

Please could you tell what provided the last suggestions you had on this post ?
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=757938

I see you have suppressed the shell part. Expect doesn't seem to match your string. This make me insist on the way of environment problems.

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Dewa Negara_4
Regular Advisor

Re: Expect script on crontab

Hi Fred,

Thanks alot for your help. I tried it, but the script still can not run well. I configured it as below.

[importve@prod:/home/ap1030/transfer]
$ crontab -l
57 16 * * * /home/ap1030/transfer/run_sftp > /tmp/sftp 2>1&

[importve@sihp8026:/home/ap1030/transfer]
$ more run_sftp
. /home/importve/.profile
cd /home/ap1030/transfer
./sftp

[importve@sihp8026:/home/ap1030/transfer]
$ more /tmp/sftp
spawn /opt/openssh/bin/sftp -b /home/ap1030/transfer/batchFile importve@www.importve.com
Connecting to www.importve.com...
importve@www.importve.com's password:

[importve@sihp8026:/home/ap1030/transfer]
$ ll
total 8
-rw-r--r-- 1 importve sys 56 Dec 2 14:44 batchFile
-rwx------ 1 importve sys 117 Dec 8 16:54 run_sftp
-rwx------ 1 importve sys 520 Dec 8 16:55 sftp
-rw-r--r-- 1 importve sys 13 Dec 2 14:43 test_sihp8026


I got this link http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=13&t=000741 that related to my issue. It mentioned that the problem with the command "interact" in expect.

Any idea, pls help me.

Thanks and Best Regards,
Dewa


Santos
Fred Ruffet
Honored Contributor

Re: Expect script on crontab

Looking the page you show... Rather than setting env like this, try to launch your command with a su like this :
05 15 * * * su - root -c "/usr/local/bin/expect -f /home/ap1030/transfer/sftp > /tmp/sftp 2>1&"
(or use any other user if you don't want to launch with root)

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Dewa Negara_4
Regular Advisor

Re: Expect script on crontab

Hi Fred,

Thanks. I tried it below. But still can not resolve the issue.

[root@prod:/home/ap1030/transfer]
# more /tmp/sftp
spawn /opt/openssh/bin/sftp -b /home/ap1030/transfer/batchFile importve@www.importve.com
Connecting to www.importve.com...
importve@www.importve.com's password:

[root@prod:/home/ap1030/transfer]
# crontab -l|grep sftp
33 18 * * * su - root -c "/usr/local/bin/expect -f /home/ap1030/transfer/sftp > /tmp/sftp 2>1&"

It always stuck after running command "interact" on the last line.

I got the information about "interact" command in expect as below :

"Interact is an Expect command that transfers control from the script to the user. When this command is executed, Expect stops reading commands from the script and instead begins reading from the keyboard."

Thanks and Best Regards,
Dewa

Santos
Fred Ruffet
Honored Contributor

Re: Expect script on crontab

I don't know expect enough, but last quote you give let me thing interact is not the godd command. If it tries to get info from keyboard, it will break as long as cron job does not have a tty.

I imagine that, by using interact, you want expect to read the file specified with -b. Have you tried replacing "-b" with "<", in order for the file to become input ? To try this, keep interact and only modify -b option.

Other way is to find an expect command that would use file provided by -b as interact uses input.

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)