[one-users] exec_and_log doesn't propagate exit status

Javier Fontan jfontan at fdi.ucm.es
Mon Aug 24 08:37:31 PDT 2009


Hello,

On Mon, Aug 24, 2009 at 4:59 PM, Sander Klous<sander at nikhef.nl> wrote:
> Hi,
> I think exec_and_log doesn't propagate the exit status of the command it
> executes. Is that true?
> So, if I do:
>
> exec_and_log "ls bla"
> echo $?
>
> I would like to see the exit status of `ls bla`.
> At the moment, I think it just returns -1 on failure.
> Is that right?

You are right, when the command fails it always returns -1 (from tm_common.sh):

--8<------
# Executes a command, if it fails return error message and exits
function exec_and_log
{
    output=`$1 2>&1 1>/dev/null`
    if [ "x$?" != "x0" ]; then
        log_error "Command \"$1\" failed."
        log_error "$output"
        error_message "$output"
        exit -1
    fi
    log "Executed \"$1\"."
}
------>8--

I think you can modify it to return the command exit code this way:

--8<------
# Executes a command, if it fails return error message and exits
function exec_and_log
{
    output=`$1 2>&1 1>/dev/null`
    code=$?
    if [ "x$code" != "x0" ]; then
        log_error "Command \"$1\" failed."
        log_error "$output"
        error_message "$output"
        exit $code
    fi
    log "Executed \"$1\"."
}
------>8--

Bye

-- 
Javier Fontan, Grid & Virtualization Technology Engineer/Researcher
DSA Research Group: http://dsa-research.org
Globus GridWay Metascheduler: http://www.GridWay.org
OpenNebula Virtual Infrastructure Engine: http://www.OpenNebula.org


More information about the Users mailing list