Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Using Expect with cron

Posted by sarmstro (sarmstro), 2 August 2005
Having a nightmare trying to get a simple expect script to run through cron. I'm spawning a ping and then expecting a regexp pattern to pull out the ip of the machine. The trouble is, as soon as the ping is spawned, it's getting an eof (I can see this through the exp_internal output). Anyone have any ideas?


Posted by admin (Graham Ellis), 2 August 2005
Does the expect script run correctly from the command line?  (I'll make an educated guess that it does    )

The output of cron jobs is usually captured by the cron system and sent on to you by email I'm wondering if this feature / capture is getting in the way ...

Also wondering if the crontab has the same PATH variable settings as the command line test - you might get the behaviour you described if ping isn't in the PATH and a "command not found" gets sent to STDOUT ....

In fact ...

testing ....


spawn ping -c 2

expect round-trip

Worked from the command line and FAILED under cron for me


spawn /sbin/ping -c 2

expect round-trip

Worked from th ecommand line and WORKED under cron for me

Posted by sarmstro (sarmstro), 2 August 2005
Thanks for the reply, Graham.

ping is in /bin in my system (debian), which is in the cron path and gets run ok. The redirected script output is as follows when run through cron:

spawn ping headline-32
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {6141}

expect: does "" (spawn_id exp11) match regular expression "[0-9]+.[0-9]+.[0-9]+.[0-9]+"? no
expect: read eof
expect: set expect_out(spawn_id) "exp0"
expect: set expect_out(buffer) ""

the sudden read eof is what's getting me. This does work fine outside of cron. I've tried setting the TERM env variable as well, as it's listed in Libes book as a pitfall.


Posted by admin (Graham Ellis), 2 August 2005
ping is one of those great utilities that seems to differ under different versions of *nix ... I know that I've had separate expect programs for Caldera and Red Hat in the past ... and I don't have a Debian system to hand to "play" with.

Perhaps the Debian ping isn't reporting the IP address in some circumstances, and that's why there's no match? If I was in your shoes, I would be printing out my expect_out (buffer)s at each step to see what I had actually received.

-- Graham

P.S.  Might be a good idea to put a \\ in front of each . in the regular expression - "literally match a ." rather than "match any character".   That will cut out the chance of certain spurious matches - e.g. to any 7 digit number.

Posted by sarmstro (sarmstro), 2 August 2005
Hi Graham,

Yeah, I thought I hadn't escaped those dots enough in the regular expression - I'll change that.

This ping script is actually run sourced from another script which doesn't itself spawn any processes, but does do some logging activities using a STAF process (not sure if you're familiar with STAF).

I've tracked it down to the fact that something which goes before, leaves a process running which causes this eof, because if I put in a catch {close} statement before running each test in the framework, it solves the problem. If I just put in "close", sometimes there is no process and it bombs.

So, still haven't really found the solution but have found a fudge around it, and still can't really explain why running through cron would make a difference to this.


Posted by sarmstro (sarmstro), 3 August 2005
Fixed it at last!

The problem was I had defined an expect_after statement:

expect_after timeout {
     log "timed out waiting for some string" ERROR
     testStep "FAIL" "timed out waiting for string - raised by expect_after"

(log and testStep are pre-defined procedures in my test automation framework)

This statement was in one of the libraries I was loading in. This somehow caused an eof to the first process I would spawn, messing up my tests.

Anyway, thought you'd like to know. Thanks for your help,

Posted by admin (Graham Ellis), 5 August 2005
Ah!   Expect is so gloriously OO that ... things like this can happen.  Although it can work with multiple processes, you've got to take care to save and restore settings such as the spawn_id if you want to do this.   Anyway - glad you're sorted

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho