Expect script cronjob running but dying prematurely

I have an Ubuntu machine that I'd like to update automatically. I've written an expect script to run the aptitude package manager and update my packages. Essentially it does:

aptitude update && aptitude upgrade

while answering "yes" at the appropriate time.

It works quite nicely when run directly from the command line. However, when the cronjob I set up runs it (as root), it dies very quickly. In an attempt to figure out what's going wrong, I 'tee'd' the output into a log. In the "aptitude update" stage, it only updates maybe 1/6 of the total sources before inexplicably terminating. It never reaches the "upgrade" stage.

Does anyone have any idea what I might be doing wrong or have any way I might be able to diagnose why the script is quitting early?

Here's the script:

#!/usr/bin/expect -f

set timeout 180
spawn aptitude update

expect {
   eof
}

set timeout 3600
spawn aptitude safe-upgrade

expect {
    "Y/n" { send "y\r"; expect { eof } } \
    eof
}

And here's my crontab line:

        *       */12    *       *       *       /home/jimmy/script/patch.sh | tee /home/jimmy/script/patch.log

why do you use " | tee " ???

I mean : it is in a cron job, so you should just :

/home/jimmy/script/patch.sh >/home/jimmy/script/patch.log 2>&1

By the way i am not very familiar with the notation you use in your cronjob definition, so i don't know if it makes sens as is.

Maybe you can try something like :

0 0,12 * * * /home/jimmy/script/patch.sh >/home/jimmy/script/patch.log 2>&1

I figured the script would still need access to STDOUT given that it acts based upon the output. As I mentioned though, that was only done after the script was found to not work.

That notation is not supported in all flavors of UNIX but Linux is more than happy with it.

Did you try to make a run logging the standard error output to a log file ? if so, what message did you get ?

---------- Post updated at 01:13 AM ---------- Previous update was at 01:11 AM ----------

Also check your environment (in your script before runing the spawn commands, log the output of an "env" command and compare the logged environment (when launched via crontab) with the one you have when you run it manually

Good ideas but neither one helped. This isn't a production box, so it's not that important. I was mostly curious as to whether I was missing something obvious. I could just pipe 'yes' into it to make it work and simplify the process. Thanks for making an effort though!