LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   expect scripts and cron (https://www.linuxquestions.org/questions/linux-software-2/expect-scripts-and-cron-107373/)

banderson 10-22-2003 09:09 PM

expect scripts and cron
 
I have an expect script that executes fine in my (roots) home directory, but not from anywhere else. I would like to create a cron job to execute the script but I don't know how to include the enviroment variable and path statements necessary for the script to run in a cron job. Anyone have any ideas?

TIA.

ToniT 10-22-2003 09:39 PM

Make a wrapper script that contains those PATH and other environment variable definitions and then calls the expect. Then call this wrapper from your crontab.

banderson 10-22-2003 10:25 PM

great can you give me an example of the wrapper script?

ToniT 10-23-2003 03:17 PM

Something like
Code:

#!/bin/bash
export PATH=$PATH:/my/custom/path:/other/custom/path
export my_strange_environmentvariable="foo"
cd /root
expect yadda-yadda..


banderson 10-24-2003 12:41 PM

k, appreciate the help, howerver it still doesn't work. I looked at my .bash_profile and got the infor for the path and bash_env.

Here is my wrapper script:

#!/bin/bash
export PATH=/usr/local/sbin:/usr/local/bin: etc...
export BASH_ENV=/root/.bashrc
cd /root
expect blah-blah

When cron runs ps will show /bin/bash process followed by another for the expect script.

What am I doing wrong. I will pay to have this work properly!

ToniT 10-25-2003 10:22 PM

The process you should see is the bash process with the child process for the expect, so that part presumably works as expected.

If i understand you correctly, the problem is, that the command run in the cron
won't get executed as it should.

While you speak in general terms without concrete example of what you are trying to do
and what in that fails, I can only give a few ideas of what might have gone wrong.
  • Is the line in crontab set up correctly? Especially what comes to my mind is that is the
    user root or something else? Apparently you want to run this script as root.
  • Does the wrapper-script work iif you run it interactively from the shell (as the direct calling of expect works)? If it doesn't, fix the script so that it works.

About the environment variables and other initialization in the startup scripts:
  • When run from the cron, blocks that are contained between the 'if [ "$PS1" ]; then' and 'fi' are not executed.
  • exporting of BASH_ENV doesn't work as you migth expect. The shell script named there is ran only for subsequent shell calls (there are none because 'expect' is not a shell script but a c-program). If you want to contain the environment setup in /root/.bashrc, you can do it by command 'source /root/.bashrc'.
  • How have you got yourself to root in the first place when you have tested the command? If you do sudo or su, some of the environment variables are inherited from the invoking user account. use 'sudo su -' or 'su -' to get a shell without environment from the invoking user [and see if it the original script still works].

ToniT 10-29-2003 09:41 AM

Forgot to mention that also /etc/profile and/or /etc/bash.bashrc are executed when run as an interactive user but not when run from cron.

banderson 10-29-2003 05:02 PM

That could be a large part of the problem couldn't it. The wrapper shell never gets to execute the expect script when it run under cron. I know this because all the expect script does it ftp files to a web server, and the files aren't copied when cron is used to execute the wrapper script.

ruchitadesai 05-27-2005 04:44 AM

Hi,
Im also facing the same problem of Expect script not running in crontab...............
My script looks like

#! /usr/bin/expect -f
set d [timestamp -format %H%d%m]
log_file -a sysusage_$d.txt
set prompt "APPL> "
global argc
if { $argc !=0 } {
send_user "Invalid number of args"
send_user "Usage: $argv0"
exit -1
}
set server_ip x.x.x.x
set app_name xyz
set timeout 5
proc do_attempt { app_name server_ip } {
global prompt
set timeout 5
log_user 0
spawn application -i $server_ip
expect "login: " { send "login\r" }
expect "password: " { send "password\r" }
expect "APPL> " { send "$app_name:;\r" }
expect {
-re "More..." {
send "\r";
exp_continue;
}
expect "APPL> " { send "exit\r" }
}
}
do_attempt $app_name $server_ip;
exit


I think within the script since im changing the prompt to "APPL>" from "$" , I need to change the environmental variable. However, plz help me how can i do this using EXPECT .


Thanks,
Ruchita


All times are GMT -5. The time now is 06:56 AM.