Rex::C...s::Run(3pm)
π¦ Rex::C...s::Run π
okay bestie, this is Rex::Commands::Run, and it's part of the User Contributed Perl Documentation collection. it's giving "run commands on remote servers" and honestly? iconic.
β¨ NAME β¨
Rex::Commands::Run β Execute a remote command. that's it. that's the whole vibe. π
πΈ DESCRIPTION πΈ
With this module you can run a command. like, that's literally it, no cap. it runs commands. slay. π₯
π SYNOPSIS π
my $output = run 'ls -l';
sudo 'id';
look at that. two lines. one runs a command, one runs it as root. main character energy fr fr. π
π CONFIGURATION AND ENVIRONMENT π
okay so please note β this is important, don't scroll past it π«Ά β Rex may set the "PATH" environment variable when executing commands on the user's behalf to a different value compared to executing the same commands manually. yes literally. your PATH might not be the PATH you think it is and it's not even being shady about it. π
the following things are available to control the related behavior:
pathcommandset_pathconfiguration optionno_path_cleanupfeature flagsource_profilefeature flagsource_global_profilefeature flag
that's the whole list bestie, no extras, we don't make things up around here. β¨
π§ EXPORTED FUNCTIONS π§
π run($command [, $callback], %options)
this function will execute the given $command and returns the output. in scalar context it returns the raw output as is, and in list context it returns the list of output lines. the exit value of the command is stored in the $? variable. yes, that $?. the OG. π«Ά
run 'uptime';
my $output = run 'uptime';
my @output_lines = run 'uptime';
so same command, three vibes: throw it away, grab it as one blobby string, or grab it as a cute little list of lines. pick your fighter. π
please note when the "tty" feature flag is enabled the combined output containing both "STDOUT" and "STDERR" is returned via "STDOUT". when using the "no_tty" feature flag, or the 1.0 feature bundle (or newer), then run() returns only the "STDOUT" output of the command. so like, know your flags, know your output, iykyk. π¦
to access separate "STDOUT" and "STDERR" output, use a callback subroutine, for example:
run 'uptime', sub {
my ( $stdout, $stderr ) = @_;
my $server = Rex::get_current_connection()->{server};
say "[$server] $stdout\n";
};
see how it hands you $stdout and $stderr as separate besties? chef's kiss. π
it also takes further options in a form of a hash. supported options are:
cwd => $pathβ Sets the working directory of the executed command to$path. basically "run this over there, not over here". πonly_if => $condition_commandβ Executes the command only if$condition_commandreturns success.unless => $condition_commandβ Executes the command if$condition_commandreturns failure. yes it's the inverse, that's the whole point bestie. πonly_notified => TRUEβ Queues the command to be executed later upon notification. it's on read, it'll get there when it gets there. β³env => { var1 => $value1, ..., varN => $valueN }β Sets environment variables for the given command.timeout => valueβ Sets the timeout for the command to be run.auto_die => TRUEβ Die if the command returns with an exit code indicating failure. It can be set globally via theexec_autodiefeature flag. dramatic? yes. useful? also yes. πcommand => $command_to_runβ If present, Rex will execute$command_to_run, and treat the first argument as an identifier for the given run() block (e.g. to be triggered with notify).creates => $file_to_createβ Tries to create$file_to_createupon execution, and skips execution if the file already exists.continuous_read => $callbackβ Calls$callbacksubroutine reference for each line of the command's output, passing the line as an argument.end_if_matched => qr{$pattern}β End execution early as soon as$patternis detected in the command's output.
Examples:
if you only want to run a command if another command succeeds or fails, use the "only_if" or "unless" options. π
run 'some-command',
only_if => 'pgrep httpd'; # only run if httpd is running
run 'some-other-command',
unless => 'pgrep httpd'; # only run if httpd is _not_ running
if you want to set custom environment variables you can do it like this:
run 'my_command',
env => {
env_var_1 => 'the value for 1',
env_var_2 => 'the value for 2',
};
if you want to end the command upon receiving a certain output:
run 'my_command',
end_if_matched => qr{$pattern};
π run($command, $arguments, %options)
this form will execute $command with the given $arguments pass as an array reference. all arguments will be quoted by Rex with "Net::OpenSSH::ShellQuoter->quoter()" according to the managed host's shell. so Rex does the quoting homework for you, because it's a good friend like that. π«Ά
run 'ls', [ '-l', '-t', '-r', '-a' ];
run 'ls', [ '/tmp', '-l' ], auto_die => TRUE;
array ref for the args, hash for the options. tidy. organized. obsessed. β¨
π run($command_description, command => $command, %options)
if you only want to run a command in certain cases, you can queue the command and notify it to trigger its execution. it's the "don't call me, i'll call you" of commands. π
run 'extract-something',
command => 'tar -C /foo -xzf /tmp/foo.tgz',
only_notified => TRUE;
# some code ...
notify 'run', 'extract-something'; # now the command gets executed
not me queuing a command and then notifying it five minutes later like it's a text i was too nervous to send. π
π¦ can_run($command)
this function checks if a command is available in the path. it accepts a list of commands, and returns the full path to the first command found. a vibe check for your binaries basically. β
task 'uptime', sub {
if ( my $cmd = can_run( 'uptime', 'downtime' ) ) {
say run $cmd;
}
};
π sudo
Run a single command, a code block, or all commands with "sudo". you need perl to be available on the remote systems to use "sudo". no perl, no sudo, sorry bestie. π
depending on your remote sudo configuration, you may need to define a sudo password with sudo_password first:
sudo_password 'my_sudo_password'; # hardcoding
it's literally marked "hardcoding" in the comments bestie, do with that information what you will π
or alternatively, since Rexfile is plain perl, you can read the password from terminal at the start:
use Term::ReadKey;
print 'I need sudo password: ';
ReadMode('noecho');
sudo_password ReadLine(0);
ReadMode('restore');
see? ReadMode('noecho') so nobody's shoulder-surfing your password. safety first, slay second. π (okay they're simultaneous actually.)
similarly, it is also possible to read it from a secret file, database, etc. get creative, you're the main character. π
you can turn sudo on globally with:
sudo TRUE; # run _everything_ with sudo
run _everything_ with sudo. everything. the whole thing. all of it. this is a big deal and it's right there in the comment. π
to run only a specific command with sudo, use :
say sudo 'id'; # passing a remote command directly
say sudo { command => 'id' }; # passing anonymous hashref
say sudo { command => 'id', user => 'different' }; # run a single command with sudo as different user
to run multiple commands with "sudo", either use an anonymous code reference directly:
sudo sub {
service 'nginx' => 'restart';
say run 'id';
};
or pass it via "command" (optionally along a different user):
sudo {
command => sub {
say run 'id';
say run 'pwd', cwd => '/home/different';
},
user => 'different',
};
note that some users receive the error "sudo: sorry, you must have a tty to run sudo". in this case you have to disable "requiretty" for this user. you can do this in your sudoers file with the following code:
Defaults:$username !requiretty
like, it literally says sorry. it's apologizing and still not letting you in. fix the requiretty thing and you're golden. β¨
and that's the whole page bestie!! π this one's running on perl v5.40.1, dated 2025-10-03, and the page itself is Rex::C...s::Run(3pm) β go run some commands, you've got this. π«ΆπΈπ
π¦ where this page comes from
- ubuntu resolute: rex 1.16.1-1 Β· Apache-2.0
- ubuntu stonking: rex 1.16.1-1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms