No::Worries::Proc(3)

πŸ“¦
version 1.7 Β· 4 versions
, in epel 9

πŸ’… 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's qx() 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 to proc_monitor() or proc_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 by proc_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 by proc_create()); PROCS can be either a single process or a reference to a list of processes; supported options:
    • "timeout" β€” the maximum number of seconds that proc_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 that proc_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 of proc_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 under proc_status in 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 SIGTERM and wait up to 1 second for the process to finish
  • if the process is still alive, send SIGINT and wait up to 1 second
  • if the process is still alive, send SIGQUIT and 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 to exec() or exit(), there is usually no need to perform any cleanup (e.g. in an END block) 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