Linux - SoftwareThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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].
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.
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 .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.