Operating System - HP-UX
1843946 Members
2199 Online
110226 Solutions
New Discussion

help with expect in shell script in cron

 
SOLVED
Go to solution
Saurabh Kapoor_2
Frequent Advisor

help with expect in shell script in cron

Hi,
I have a shell script which starts with calling an expect script that ftps a file.Then the awk scipt does some parsing on this file and finally again an expect script is called to run sqlldr to load the data into a tabele in oracle. When I run this script from command line everything runs as desired. But when I put it in cron to run every 5 minutes then it fails. I see in the cron log file rc=127. what does that mean and please provide any way to make this work.
Cant start OTS
9 REPLIES 9
Chris Wilshaw
Honored Contributor

Re: help with expect in shell script in cron

Does you cron command log to a separate file?

eg:

00 00 * * * your_script > your_log 2>&1

If not, it is worth adding this so that you capture as much information as possible.

If you aren't already logging, some information should be passed to the mail file for the user running the job - check in /var/mail

You'll probably find that the cause of the problem is an environment variable that is set when you run the script from the command line, but not when you run it from cron.
Pete Randall
Outstanding Contributor

Re: help with expect in shell script in cron

Most likely this is because of cron's minimalist environment. Make sure that you reference the full path names to all commands and specifically set any environment variables that you need.


Pete

Pete
Muthukumar_5
Honored Contributor

Re: help with expect in shell script in cron

rc=127 is saying that you are trying to use the non-shell commands.

tst; echo $?
ksh: tst: command not found
127

Try to redirect all results of the shell script to a log file. Use set -x to debug the shell script.


Easy to suggest when don't know about the problem!
Saurabh Kapoor_2
Frequent Advisor

Re: help with expect in shell script in cron

Hi all,
Thanks for your responses.So now what I started doing was to break my complete solution into parts and start testing it.
I have the follwoing expect script which tries to ftp. When it is called from command line it just works perfectly fine but it is failing in cron. In crontab -e I have given it as /usr/local/bin/expect /home/saurabh/ftp
So I have specified the complete paths.
I also tried to redirect the output toa file using the command as below in cron, but it does not log anything into the croneradsout file.

02,08,14,20,26,32,38,44,50,56 0-23 1-31 1-12 0-6 /usr/local/bin/expect /home/saurabh/scripts/erads/MRerads/test/MReradsftp /tmp/croneradsout 2>&1

spawn "/bin/sh"
send "ftp \r"
sleep 1
expect "Name\r"
send "node1\r"
sleep 1
expect "Password"
send "123\r"
sleep 2
expect "ftp"
send "cd \$dsmscm.trainer\r"
sleep 2
expect ""
send "ascii\r"
sleep 3
expect ""
send "get ECURRENT \r"
sleep 7
expect "ftp"
send "bye\r"
send "\r\r"
Cant start OTS
Saurabh Kapoor_2
Frequent Advisor

Re: help with expect in shell script in cron

Please reply guys...
Cant start OTS
Cesare Salvioni
Trusted Contributor
Solution

Re: help with expect in shell script in cron

If this is the line you are running in crontab, seems to me it is missing a >

i mean the command should be

/expect /script > logfile 2>&1

do you have it in crontab?

Also to automate ftp you don't need expect, try the script attached, changing username, password and ftpserver as needed

hope it helps
Sanjay_6
Honored Contributor

Re: help with expect in shell script in cron

Hi,

Execute your profile before the script in the cron job,

02,08,14,20,26,32,38,44,50,56 0-23 1-31 1-12 0-6 . /etc/profile;. ./.profile; /usr/local/bin/expect /home/saurabh/scripts/erads/MRerads/test/MReradsftp >/tmp/croneradsout 2>&1

Hope this helps.

Regds
Saurabh Kapoor_2
Frequent Advisor

Re: help with expect in shell script in cron

Hi Cesare,
Thanks for your Ftp script..that works perfectly and I could get rid of expect over there.
Now I put it in cron and it works there too...
I will have to now only get rid of expect for sqlldr.
Cant start OTS
Cesare Salvioni
Trusted Contributor

Re: help with expect in shell script in cron

HI.
Some days spent in vacation. One more hint, you really need expect only when you have to automate a process in a way that could change (ex. my script would not be usable if you want to take care about errors while running ftp: server not responding, file not found etc.) or when the process you are running does not use standard input in standard way (ex. it closes it and reopen before using, see the command passwd).
In any other case the concept of my script (called hereby document) is general and can be use with any command
ex.

cat < pippo
This text will be put in the file pippo
the variable $HOME will be substituted
the command $(pwd) will be substituted
ENDOFCAT

This is to say that probably you can write a script to automate both operation, ftp and access to db if you meet conditions exposed before

hope this helps