1

I have the following code. How can I redirect the output of the expect to a log file to see if the scp has copied everything successfully. This piece of my code runs in background.

    export L_SRC_SID L_SID MOUNT_POINT SERVICE_PASS APP_SERVER_1 SRC_MOUNT_POINT

    #And now transfer the file over
    /usr/bin/expect -c '
    set timeout -1
    cd /$env(MOUNT_POINT)/$env(L_SID)/apps
    spawn scp -pr apps$env(L_SRC_SID)@$env(APP_SERVER_1):/$env(SRC_MOUNT_POINT)/$env(L_SRC_SID)/apps/* .
    expect {
            yes/no { send yes\r ; exp_continue }
            password: { send $env(SERVICE_PASS)\r }
    }
    expect eof
    '
2

Simply redirect the output of your invocation of expect:

/usr/bin/expect -c '
set timeout -1
cd /$env(MOUNT_POINT)/$env(L_SID)/apps
spawn scp -pr apps$env(L_SRC_SID)@$env(APP_SERVER_1):/$env(SRC_MOUNT_POINT)/$env(L_SRC_SID)/apps/* .
expect {
        yes/no { send yes\r ; exp_continue }
        password: { send $env(SERVICE_PASS)\r }
}
expect eof
' >> /path/to/logfile 2>> /path/to/errorfile

Or, if you only want to capture the output of scp, redirect only its output:

/usr/bin/expect -c '
set timeout -1
cd /$env(MOUNT_POINT)/$env(L_SID)/apps
spawn scp -pr apps$env(L_SRC_SID)@$env(APP_SERVER_1):/$env(SRC_MOUNT_POINT)/$env(L_SRC_SID)/apps/* . >> /path/to/logfile 2>> /path/to/errorfile
expect {
        yes/no { send yes\r ; exp_continue }
        password: { send $env(SERVICE_PASS)\r }
}
expect eof
'
2

expect has an oddly named log_file procedure that (drumroll...) logs to files so you can usually just say

#!/usr/bin/env expect
set timeout -1
log_file blah.log
...

within the TCL code. See log_file (and also log_user) in the expect(1) man page for details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.