I want to execute a series of Expect scripts. The chain of execution goes like this: Cron job starts a bash script, this wrapper starts ~11 bash scripts in Parallel - each of them call an Expect script.

All is well until the Expect portion. I have the Parallel portion in the 2nd Bash script spitting out the hostnames for each expect script into a file. The do in fact appear in groups, but the Expect script dies immediately.

I have heard that due to the nature of Cron jobs I need to pass environment variables to the job. I've tried this so far:

48 9 * * * rm /home/admin/.flag && /home/admin/.profile; /home/admin/nodeTest.sh

nodeTest's significant code is:

for i in {1..10}; do
                for chunk in `ls /home/admin/assets/sagLogs/x*`; do
                        cat $chunk | parallel -j 11 -I% --max-args 1 /home/abdmin/oneNode.sh "%"

The chunks are 11 hostnames long. They get put into the parallel program to start the Expect script via oneNode - oneNode just translates the hostname to IP and determines which script to run.

This works just fine from the commandline; just the Cron job that's giving me grief. I've read that the interact tcl command causes issues, but I don't use it in my scripts. This is the line that is run in parallel:

timeout 80 sudo expect /home/admin/assets/activeTest $sag $ip &

Update: changed the previous line to:

timeout 80 sudo /usr/bin/expect /home/admin/assets/activeTest $sag $ip &

Update: After inspecting errors from the Expect script, what I'm running into is a lack of imported env variables. The first error occurs because the TERM variable is == unknown:

'unknown': I need something more specific.
    while executing
"exec /usr/bin/clear"
    invoked from within
"puts [exec /usr/bin/clear]"
    (file "/home/admin/assets/activeTest" line 35)

When I dump env from the calling script (oneNode) I can see term is set:


So it's set in the wrapper, but it doesn't get inherited by the expect script. What to do?

1 Answer 1


This is wrong

48 9 * * * rm /home/admin/.flag && /home/admin/.profile; /home/admin/nodeTest.sh

Most crucially, you execute .profile as a command -- that executes it in a separate process and when that process exits, all environment modification disappear with it.

Also, what happens if .flag doesn't exist? You won't process the profile, but you still run the nodeTest script.

I suggest you want to source the .profile. Use rm -f so no errors occur if the file is missing. And only execute the script if there are no errors in the .profile.

48 9 * * * rm -f /home/admin/.flag; . /home/admin/.profile && /home/admin/nodeTest.sh
# ..................................^
# source the .profile: execute it in the _current_ shell

Next, don't parse ls. Change

            for chunk in `ls /home/admin/assets/sagLogs/x*`; do
                    cat $chunk | ...


            for chunk in /home/admin/assets/sagLogs/x*; do
                    cat "$chunk" | ...
  • Thank you! I'll give it a go.
    – KuboMD
    Commented Jul 15, 2019 at 14:57
  • Hi Glenn - this didn't seem to work entirely. My expect scripts die very quickly because of lines like puts [exec clear] and lines that point to one of my scripts that uses the tput program. Any ideas?
    – KuboMD
    Commented Jul 15, 2019 at 17:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .