No::Worries::Proc(3)
π No::Worries::Proc β process handling without worries
ok bestie, this is a User Contributed Perl Documentation page for No::Worries::Proc(3), and it's literally about making process handling not a whole crisis. pull up a chair π«Ά
β¨ SYNOPSIS β¨
use No::Worries::Proc qw(proc_run proc_create proc_monitor proc_detach);
# simple interface to execute a command
$status = proc_run(command => [ "foo", "-x", 7 ]);
printf("foo exited with %d\n", $status);
# idem but with output redirection and more information
%proc = proc_run(command => [ qw(uname -a) ], stdout => \$output);
printf("process %d output is %s\n", $proc->{pid}, $output);
# start two process and wait for them to finish
$p1 = proc_create(
command => \@cmd1,
timeout => 5, # to be killed if still running after 5s
stderr => "/dev/null", # discard stderr
);
$p2 = proc_create(
command => \@cmd2,
stdout => \$output, # get stdout+stderr in $output
stderr => "", # merge stderr with stdout
);
proc_monitor([ $p1, $p2 ], timeout => 10);
printf("%d finished\n", $p1->{pid}) if $p1->{stop};
printf("%d finished\n", $p2->{pid}) if $p2->{stop};
# detach ourself to run as a daemon
proc_detach(callback => sub { print("started with pid $_[0]\n")});
like, that's the whole vibe right there π isn't it gorgeous?
πΈ DESCRIPTION πΈ
This module eases process handling by providing high level functions to start, monitor and stop processes. and the best part? All the functions die() on error. no silent fails, no mysterious nothing-happened, no gaslighting π
It also provides the $No::Worries::Proc::Transient variable that indicates, after a fork(), which process is transient and is about to exec() or exit(). This is useful for instance in an END block:
END {
# remove our pid file unless we are transient
pf_unset($pidfile) unless $No::Worries::Proc::Transient;
}
so you don't do cleanup for a process that's literally on its way out. iconique behavior tbh π¦
π FUNCTIONS π
ok so this module provides the following functions, and heads up: none of them being exported by default (you gotta ask, bestie, nothing is free π):
proc_output(COMMAND...)β execute the given command, capture its output (stdout only), check its exit code (report an error if it is not zero) and return the captured output; this is similar to Perl'sqx()operator but bypassing the shell and always checking the exit code πproc_create(OPTIONS)β create a new process that will execute the given command and return a hash reference representing this process (see the "PROCESS STRUCTURE" sections for more information), to be given toproc_monitor()orproc_terminate()afterwards; supported options:"command"β the command to execute, it must be an array reference"cwd"β the current working directory of the new process"timeout"β the maximum number of seconds that the process is allowed to take to run (can be fractional); after this, it may be killed byproc_monitor()"kill"β how to "gently" kill the process, see below"stdin"β what to do with stdin, see below"stdout"β what to do with stdout, see below"stderr"β what to do with stderr, see below
proc_terminate(PROC[, OPTIONS])β terminate the given process (PROC can be either a process structure or simply a process id) by sending signals and waiting for the process to finish; supported options:"kill"β how to "gently" kill the process, see below
proc_monitor(PROCS[, OPTIONS])β monitor the given process(es) (as created byproc_create()); PROCS can be either a single process or a reference to a list of processes; supported options:"timeout"β the maximum number of seconds thatproc_monitor()should take, can be fractional"bufsize"β the buffer size to use for I/O operations (default:8192)"deaths"β the minimum number of process deaths thatproc_monitor()will wait for before returning
lowkey obsessed with the "deaths" option, it's giving gothic. anyway it's just how many processes have to die before it comes back π€
proc_run(OPTIONS)β execute the given process (i.e. create and monitor it until termination) and return its status (i.e.$?) in scalar context or the whole process structure in list context; supported options: the ones ofproc_create()proc_detach([OPTIONS])β detach the current process so that it becomes a daemon running in the background (this implies forking and re-opening std*); supported options:proc_status(STATUS)β return a string representation of the given process status (i.e.$?)"callback"β code reference that will be executed by the parent process just before exiting and will be given the child pid
yeah, that"callback"bullet is sitting underproc_statusin the original β i'm just telling you exactly what's on the page, bestie, don't shoot the messenger π
π§ PROCESS STRUCTURE π§
The process structure (hash) used in this module has the following fields:
"command"β the command being executed, as an array reference"pid"β the process id"start"β the start time, in fractional seconds"stop"β the stop time, in fractional seconds"status"β the status (i.e.$?)"timeout"β true if the process has been killed because of timeout
six fields, all bangers, no filler π
β FILE DESCRIPTOR REDIRECTION β
When using the "stdin" option of proc_create(), the value can be:
- a string: input will be read from the given file name
- a scalar reference: input will be the scalar itself
When using the "stdout" and "stderr" options of proc_create(), the value can be:
- a string: output will be written to the given file name
- a scalar reference: output will be stored in the scalar
- a code reference: each time new output is available, the code will be called with two parameters: the process structure and the new output
In addition, "stderr" can also be given an empty string that means that stderr should be merged with stdout. yes, the literal empty string "" β that's the merge button, that's the slay button, don't overthink it π
π₯ KILL SPECIFICATION π₯
Both proc_create() and proc_terminate() can be given a "kill" option that specifies how the process should be killed. (gently. allegedly.)
The specification is a string containing a space separated list of signal/grace couples, meaning: send the given signal and wait a bit for the process to finish.
If not specified, the default is "TERM/1 INT/1 QUIT/1", meaning:
- send
SIGTERMand wait up to 1 second for the process to finish - if the process is still alive, send
SIGINTand wait up to 1 second - if the process is still alive, send
SIGQUITand wait up to 1 second - if the process is still alive, send
SIGKILL(implicit)
ok so this is the escalation ladder and it is NOT a vibe check you want to fail four times π note the SIGKILL at the end is implicit β you don't ask for it, it just happens.
π GLOBAL VARIABLES π
This module uses the following global variables (none of them being exported β same rule as the functions, bestie):
$Transientβ true if the process is about toexec()orexit(), there is usually no need to perform any cleanup (e.g. in anENDblock) for this kind of process
π SEE ALSO π
No::Worries.
that's it. that's the whole list. sometimes less is more β¨
π AUTHOR π
Lionel Cons <http://cern.ch/lionel.cons>
Copyright (C) CERN 2012-2019
ok that's the page!! π it's perl v5.32.1, dated 2021-10-18, and it lives at No::Worries::Proc(3) β so go forth, fork responsibly, and remember: no worries, bestie π
π¦πΈ
π¦ where this page comes from
- epel 9: perl-No-Worries 1.7-1.el9 Β· GPL+ or Artistic
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms