Expect script not executing via cron

Hello All,

I'm having an issue getting an expect script to run as a cron job. The script executes fin if I run it from the command line but I get nothing when trying to run it as a cron job. I've researched other forums and threads and there have been references to the environment, or lack thereof through cron, but I still haven't been able to resolve. The script is as follows:

------------------------------------------------------------------------
Script:

#!/usr/bin/expect -f
 #export PATH=/usr/bin:TH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
 
spawn "/home/scripts/myscript.sh"
 expect "Select Operation" { send "1\r" }
 expect "Select TMS6 Snapshot Type" { send "1\r" }
 set timeout 600
expect eof
exit

-----------------------------------------------------------------------
cron job:

41 13 * * * root /home/user/expect.sh

---------------------------------------------------------------------
output from cron log:

Jul 19 13:40:06 localhost crond[4862]: (CRON) STARTUP (V5.0)
Jul 19 13:41:01 localhost crond[4997]: (root) CMD (/home/user/expect.sh)

----------------------------------------------------------------------

The expect script will pass 2 arguments into myscript.sh, which then creates an output file. When I run this on the command line, no problems, but through cron.....no results. Thanks for the help in advance!

---------- Post updated at 03:28 PM ---------- Previous update was at 03:11 PM ----------

my apologies.....I'm a newb to forum posting.

You need to remove the # from in front of the export to actually change your PATH. As is, cron will be running it with a very minimal path.

Thanks for the reply Corona688. I must've pasted an older version of the script. Either way, I removed the # but the script continues to give no output or result. Just the same cron log entry showing the job getting kicked off and nothing else. Here's an updated version of what the script looks like:

#!/usr/bin/expect -f
export PATH=/usr/bin:TH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
 spawn "/home/scripts/myscript.sh"
 expect "Select Operation" { send "1\r" }
 expect "Select TMS6 Snapshot Type" { send "1\r" }
 set timeout 600
expect eof
exit

Ah, I think I see. 'export' is a shell command, not an expect one.

I think for expect/TCL it's something like

setenv PATH "/usr/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin"

The TH= does not belong in PATH and I'm not sure what it was intended to do. Neither should PATH entries be repeated like that.

I picked up that line with the TH in the path from another thread in a futile attempt to resolve this. With your suggested changes the script still doesn't run via cron and now it no longer runs on the command line and produces the following output:

 invalid command name "env"
    while executing
"env PATH "/usr/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin""
    (file "./expect.sh" line 2)
 

I've looked at a few other forums and tried using a variant of env PATH "/usr/bin......." but end up with the same result of nothing. Damn you cron and your lack of environment

I don't think its a PATH or environment problem, as no command is run or called inside the expect script, and expect is run with full path.

I'm a bit baffled. The form of your crontab entry indicates it is from the system crontab file, the only one that requires a user to be run as. Please confirm that it comes from there (most probably /etc/crontab ) and is not used as a user crontab. Although the syslog line shows it is really starting...

Can you try to write a verbose log file and post that here?

Rudi, I tested your theory and I believe you are correct with the path/environment. I ran the following:

command line:

PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/usr/sbin:

via cron:

PATH=/usr/bin:/bin

output from which expect:

/usr/bin/expect

Based on this I'd say that it's not a path or environment issue, which led me to your 2nd problem area....the cron entry file. I've tried running it from both the /etc/crontab, crontab -e (as local user and root) but it seems the job never runs despite the log posting it starts.

I'm not quite sure on how to produce a verbose log file...I'll be searching for that process now.

You didn't overlook the obvious - adapt the cron start time?

Any other ideas on this one if its not the environment? This is the final hurdle I need to jump to close out a project.

---------- Post updated at 08:46 AM ---------- Previous update was at 08:41 AM ----------

Apologies RudiC....I didn't see there was a pg 2. I've been adjusting the start times of the cron jobs. I've gone as far as substituting the expect script with a simple bash file creation script just to confirm the cron was running correctly. The job will run with a bash script but not the expect.

As proposed before, check if the cron job is running at all. You may want to start with a very simple approach, e.g. a mere pwd command, and check its output.

OK, put the expect stuff into a bash script...

I'll give that a shot now. In researching it looks like the syntax is a little different but I'll work through it and update. Thank you for the help btw.

That is not the code I suggested. Try 'setenv' like it was shown.

I've tried using bot env and setenv and both have the same outcome. I'm questioning that it's an environment problem as both the cron job and the command line reference the /usr/bin/ directory

It shouldn't be the same outcome, the error you showed specifically complains about 'set', not 'setenv'.

Can you show us your cron line?

script:

#!/usr/bin/expect -f
setenv PATH "/usr/bin:/usr/local/sbin"
 spawn "/tms6/scripts/tms6-snapshot.sh"
 expect "Select Operation" { send "1\r" }
 expect "Select TMS6 Snapshot Type" { send "1\r" }
 set timeout 600
expect eof
exit

output from command line:

invalid command name "setenv"
    while executing
"setenv PATH "/usr/bin:/usr/local/sbin""
    (file "./expect.sh" line 2)

cron:

16 13 * * * root /home/tms6/expect.sh

How about:

set env(PATH) "/usr/bin:/usr/local/sbin"