Metainformationen zur Seite

LoxBerry::System::execute

($exitcode, $output) = execute( %params );

This function executes a shell command, and returns exitcode and it's output. With named parameters, also logging is done into a LoxBerry::Log object.

LoxBerry 2.0

This function is available starting with LoxBerry V2.0. If you use that function, set this minimum version in your plugin.cfg.

The function always returns the exitcode and the output of the command. With additional parameters, you can directly log the execution state.

The function requires a hash of parameters. If no hash, but only a single string is given, this string is used as the command.

Response

The function returns the exitcode and the output of the command.

If you want to receive one or both return values, it is mandatory to use the round brackets:

my ($exitcode) = execute(...);

my ($exitcode, $output) = execute(...);

my (undef, $output) = execute(...);

Abstract

use LoxBerry::System;
 
# Simply execute a command, without anything
my ($exitcode) = execute( "ls -l /opt/loxberry" );
 
# Get also the output of ls
my ($exitcode, $output) = execute( "ls -l /opt/loxberry" );
 
# Use a log object to log to
my ($exitcode, $output) = execute( { 
    command => "ls -l /opt/loxberry",
    log => $log
} );
 
# Log to a LoxBerry::Log object
# Default messages are written as intro text, for success or failure.
my ($exitcode) = execute( { 
    command => "ls -l /opt/loxberry",
    log => $log,
} );    
 
# Log to a LoxBerry::Log object
# ignoreerrors: Independent of the execution result, always OK is logged 
# Available from LoxBerry 3.0
my ($exitcode) = execute( { 
    command => "ls -l /opt/loxberry",
    log => $log,
    ignoreerrors => 1
} );    
 
# Userdefined messages in the log
my ($exitcode) = execute( { 
    command => "ls -l /opt/loxberry",
    log => $log,
    intro => "Getting directory listing...",
    ok => "Directory listing received successfully",
    error => "Could not query directory listing."
} );    
 
# Usually, exitcode 0 is OK. You can change the exitcode that should identify OK
my ($exitcode) = execute( { 
    command => "ls -l /opt/loxberry",
    log => $log,
    intro => "Getting directory listing...",
    ok => "Directory listing received successfully",
    error => "Could not query directory listing.",
    okcode => 100
} );    
 
# On error, an ERROR event is written to the log. Use the warn instead of the error parameter to WARN.
my ($exitcode) = execute( { 
    command => "ls -l /opt/loxberry",
    log => $log,
    intro => "Getting directory listing...",
    ok => "Directory listing received successfully",
    warn => "Could not query directory listing.",
} );

Parameter List

Parameter Mandatory Description
command yes The command to be executed.
log A LoxBerry::Log object
intro log objectThe message logged before the execution (LOGINF)
ok log objectThe message logged on success (LOGOK)
error log objectThe message logged on error (LOGERR). The parameter is mutually exclusive with the warn parameter.
warn log objectThe message logged on error (LOGWARN). On error, only the severity LOGWARN instead of LOGERR is used.  The parameter is mutually exclusive with the error parameter.
okcode log objectDefault is 0 (exitcode 0 = LOGOK). Change the exitcode, that represents a successful execution. All other exitcodes are an error.
ignoreerrors Regardless of the exitcode, always an LOGOK message is logged. Available from LoxBerry 3.0 (parameter is ignored below LB3.0)