LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-22-2003, 09:09 PM   #1
banderson
Member
 
Registered: Oct 2003
Location: Salt Lake City, UT
Distribution: RedHat 9
Posts: 35

Rep: Reputation: 15
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.
 
Old 10-22-2003, 09:39 PM   #2
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
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.
 
Old 10-22-2003, 10:25 PM   #3
banderson
Member
 
Registered: Oct 2003
Location: Salt Lake City, UT
Distribution: RedHat 9
Posts: 35

Original Poster
Rep: Reputation: 15
great can you give me an example of the wrapper script?
 
Old 10-23-2003, 03:17 PM   #4
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
Something like
Code:
#!/bin/bash
export PATH=$PATH:/my/custom/path:/other/custom/path
export my_strange_environmentvariable="foo"
cd /root
expect yadda-yadda..
 
Old 10-24-2003, 12:41 PM   #5
banderson
Member
 
Registered: Oct 2003
Location: Salt Lake City, UT
Distribution: RedHat 9
Posts: 35

Original Poster
Rep: Reputation: 15
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!
 
Old 10-25-2003, 10:22 PM   #6
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
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].

Last edited by ToniT; 10-25-2003 at 10:23 PM.
 
Old 10-29-2003, 09:41 AM   #7
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
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.
 
Old 10-29-2003, 05:02 PM   #8
banderson
Member
 
Registered: Oct 2003
Location: Salt Lake City, UT
Distribution: RedHat 9
Posts: 35

Original Poster
Rep: Reputation: 15
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.
 
Old 05-27-2005, 04:44 AM   #9
ruchitadesai
LQ Newbie
 
Registered: Mar 2005
Location: India
Posts: 7

Rep: Reputation: 0
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What are you're best Cron scripts? deception Programming 7 11-11-2005 06:37 PM
cron scripts kirmet Programming 1 10-20-2005 12:37 PM
cron job for expect script ruchitadesai Programming 9 05-27-2005 04:34 AM
How to retrieve email from cron scripts ? wallywood Linux - General 1 03-05-2004 03:34 PM
using expect scripts for remote admin jmr71769 Linux - General 3 01-21-2004 01:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 04:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration