aspose file tools*
The moose likes Linux / UNIX and the fly likes Expect script on crontab Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Expect script on crontab" Watch "Expect script on crontab" New topic
Author

Expect script on crontab

Dewa Negara
Greenhorn

Joined: Dec 08, 2004
Posts: 3
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?

Below is my crontab entry and the log file. Pls help.

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

[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:

Thanks and Best Regards,
Dewa
Peter Rooke
Ranch Hand

Joined: Oct 21, 2004
Posts: 794

Yes, if you are running this as a deamon process, via cron, then the script will not have any user to interact with.

From the website: Expect Tutorial

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

Have a visit to this web site, it gives examples of how to automate ftp, rlogin etc.

Lastly have you looked at autoexpect: autoexpect - generate an Expect script from watching a session - you could autoexpect, then do your ftp task from within the shell, exit then have a look at the expect file that has been generated. Gives a good starting point.


Regards Pete
Dewa Negara
Greenhorn

Joined: Dec 08, 2004
Posts: 3
Hi Peter,

Thanks alot for your input. I do not have knowlegde in expect script. When I read the autoexpect documentation, I did not get the point.

Could you pls help me to make my script running using cron? What command should I add the expect script to make it running through cron?

Pls help.

Thanks and best regards,
Dewa
Peter Rooke
Ranch Hand

Joined: Oct 21, 2004
Posts: 794

Dewa, have a look at the expect tutorial.

Typically you will use the 'send' command to mimic user input. Don't forget the '\r' on the end, for carriage return.

Next use the 'expect' command, this allows the script to respond to the operating systems prompts. For example:

Really you need to get a better understanding of Linux / Unix, before you mess around with Cron. Try 'man cron', 'man crontab', or the manuals provided with the operating system. A few years back, when doing telephone customer support, I once asked a customer to edit the crontab file with the VI editor - it all when badly wrong. Never did that again!
[ December 08, 2004: Message edited by: Peter Rooke ]
Dewa Negara
Greenhorn

Joined: Dec 08, 2004
Posts: 3
Hi Peter,

Thanks alot for your input. If you checked my script above, I use "send" "expect" command as below :

expect "password:"
send "$authLine\n";
interact

FYI that I do not have problem with cron or running a job from cron. My issue is that how can I run my sftp script with "interact" in the last line, so it can be running on cron. Because so far It is running well when I run it manually. But it is not running fine using cron.

Pls help if you have an idea how I can overcome "interact" issue in the last line.

Thanks and Have a nice day.

Thanks and Best Regards,
Dewa
Peter Rooke
Ranch Hand

Joined: Oct 21, 2004
Posts: 794

It should be possible to get the expect script to issue a 'mail' command, this will be devivered to you. Then get the script to interact with the mail system, and get its input from the reply that you send.

This is a lot of work mind.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Expect script on crontab
 
Similar Threads
Sun Solaris : setting the classpath?
creating a cron file
Class Not Found exception in cron job
jdk1.3.0_02 PATH????????????
problem in running a expect script from crontab