NAME

govproc-pericmd - Perinci::CmdLine-based variant of govproc CLI

VERSION

This document describes version 0.001 of govproc-pericmd (from Perl distribution App-govproc-pericmd), released on 2019-05-22.

SYNOPSIS

Use as you would use govproc:

 % govproc-pericmd --euid tono --timeout '2.5 hours' ...

DESCRIPTION

This script is just like govproc but uses the Perinci::CmdLine CLI framework to give you conveniences like shell tab completion and argument coercions (see the above example in Synopsis).

OPTIONS

* marks required options.

Main options

--command-json=s

Command to run (JSON-encoded).

See --command.

--command=s@*

Command to run.

Passed to <pm:IPC::Run>'s `start()`.

Can be specified multiple times.

--killfam

Instead of kill, use killfam (kill family of process).

This can be useful e.g. to control load more successfully, if the load-generating processes are the subchildren of the one we're governing.

This requires <pm:Proc::Killfam> CPAN module, which is installed separately.

--name=s

Should match regex `\A\w+\z`. Used in several places, e.g. passed as `prefix` in <pm:File::Write::Rotate>'s constructor as well as used as name of PID file.

If not given, will be taken from command.

--pid-dir=s

Directory to put PID file in.

Configuration options

--config-path=filename, -c

Set path to configuration file.

--config-profile=s, -P

Set configuration profile to use.

--no-config, -C

Do not use any configuration file.

Environment options

--no-env

Do not read environment for default options.

Instance control options

--on-multiple-instance=s

Valid values:

 ["exit"]

Can be set to `exit` to silently exit when there is already a running instance. Otherwise, will print an error message `Program <NAME> already running`.

--single-instance

If set to true, will prevent running multiple instances simultaneously. Implemented using <pm:Proc::PID::File>. You will also normally have to set `pid_dir`, unless your script runs as root, in which case you can use the default `/var/run`.

Load control options

--load-check-every=s

Frequency of load checking.

Default value:

 10
--load-high-limit-json=s

See --load-high-limit.

--load-high-limit=s

Limit above which program should be suspended, if load watching is enabled. If integer, will be compared against <pm:Unix::Uptime>`->load`'s `$load1` value. Alternatively, you can provide a custom routine here, code should return true if load is considered too high.

Note: `load_watch` needs to be set to true first for this to be effective.

--load-low-limit-json=s

See --load-low-limit.

--load-low-limit=s

Limit below which program should resume, if load watching is enabled. If integer, will be compared against <pm:Unix::Uptime>`->load`'s `$load1` value. Alternatively, you can provide a custom routine here, code should return true if load is considered low.

Note: `load_watch` needs to be set to true first for this to be effective.

--load-watch

If set to 1, enable load watching. Program will be suspended when system load is too high and resumed if system load returns to a lower limit.

Logging options

--debug

Shortcut for --log-level=debug.

--log-level=s

Set log level.

--log-stderr-json=s

Will be passed as arguments to `File::Write::Rotate` (JSON-encoded).

See --log-stderr.

--log-stderr=s

Will be passed as arguments to `File::Write::Rotate`.

Specify logging for STDERR. Logging will be done using <pm:File::Write::Rotate>. Known hash keys: `dir` (STR, defaults to `/var/log`, directory, preferably absolute, where the log file(s) will reside, should already exist and be writable, will be passed to <pm:File::Write::Rotate>'s constructor), `size` (int, also passed to <pm:File::Write::Rotate>'s constructor), `histories` (int, also passed to <pm:File::Write::Rotate>'s constructor), `period` (str, also passed to <pm:File::Write::Rotate>'s constructor).

--log-stdout-json=s

Will be passed as arguments to `File::Write::Rotate` (JSON-encoded).

See --log-stdout.

--log-stdout=s

Will be passed as arguments to `File::Write::Rotate`.

--quiet

Shortcut for --log-level=error.

--trace

Shortcut for --log-level=trace.

--verbose

Shortcut for --log-level=info.

Output control options

--no-show-stderr

Can be used to turn off STDERR output. If you turn this off and set `log_stderr`, STDERR output will still be logged but not displayed to screen.

--no-show-stdout

Output options

--format=s

Choose output format, e.g. json, text.

Default value:

 undef
--json

Set output format to json.

--naked-res

When outputing as JSON, strip result envelope.

Default value:

 0

By default, when outputing as JSON, the full enveloped result is returned, e.g.:

    [200,"OK",[1,2,3],{"func.extra"=>4}]

The reason is so you can get the status (1st element), status message (2nd element) as well as result metadata/extra result (4th element) instead of just the result (3rd element). However, sometimes you want just the result, e.g. when you want to pipe the result for more post-processing. In this case you can use `--naked-res` so you just get:

    [1,2,3]

Restart options

--restart

If set to true, do restart.

Screensaver options

--no-screensaver

Prevent screensaver from being activated.

Setuid options

--egid=s

Set EGID(s) of command process.

Need to be root to be able to setuid.

--euid=s

Set EUID of command process.

Need to be root to be able to setuid.

Timeout options

--timeout=s

Apply execution time limit, in seconds.

After this time is reached, process (and all its descendants) are first sent the TERM signal. If after 30 seconds pass some processes still survive, they are sent the KILL signal.

The killing is implemented using <pm:IPC::Run>'s `kill_kill()`.

Upon timeout, exit code is set to 124.

Other options

--help, -h, -?

Display help message and exit.

--version, -v

Display program's version and exit.

COMPLETION

This script has shell tab completion capability with support for several shells.

bash

To activate bash completion for this script, put:

 complete -C govproc-pericmd govproc-pericmd

in your bash startup (e.g. ~/.bashrc). Your next shell session will then recognize tab completion for the command. Or, you can also directly execute the line above in your shell to activate immediately.

It is recommended, however, that you install modules using cpanm-shcompgen which can activate shell completion for scripts immediately.

tcsh

To activate tcsh completion for this script, put:

 complete govproc-pericmd 'p/*/`govproc-pericmd`/'

in your tcsh startup (e.g. ~/.tcshrc). Your next shell session will then recognize tab completion for the command. Or, you can also directly execute the line above in your shell to activate immediately.

It is also recommended to install shcompgen (see above).

other shells

For fish and zsh, install shcompgen as described above.

CONFIGURATION FILE

This script can read configuration files. Configuration files are in the format of IOD, which is basically INI with some extra features.

By default, these names are searched for configuration filenames (can be changed using --config-path): ~/.config/govproc-pericmd.conf, ~/govproc-pericmd.conf, or /etc/govproc-pericmd.conf.

All found files will be read and merged.

To disable searching for configuration files, pass --no-config.

You can put multiple profiles in a single file by using section names like [profile=SOMENAME] or [SOMESECTION profile=SOMENAME]. Those sections will only be read if you specify the matching --config-profile SOMENAME.

You can also put configuration for multiple programs inside a single file, and use filter program=NAME in section names, e.g. [program=NAME ...] or [SOMESECTION program=NAME]. The section will then only be used when the reading program matches.

Finally, you can filter a section by environment variable using the filter env=CONDITION in section names. For example if you only want a section to be read if a certain environment variable is true: [env=SOMEVAR ...] or [SOMESECTION env=SOMEVAR ...]. If you only want a section to be read when the value of an environment variable has value equals something: [env=HOSTNAME=blink ...] or [SOMESECTION env=HOSTNAME=blink ...]. If you only want a section to be read when the value of an environment variable does not equal something: [env=HOSTNAME!=blink ...] or [SOMESECTION env=HOSTNAME!=blink ...]. If you only want a section to be read when an environment variable contains something: [env=HOSTNAME*=server ...] or [SOMESECTION env=HOSTNAME*=server ...]. Note that currently due to simplistic parsing, there must not be any whitespace in the value being compared because it marks the beginning of a new section filter or section name.

List of available configuration parameters:

 command (see --command)
 egid (see --egid)
 euid (see --euid)
 format (see --format)
 killfam (see --killfam)
 load_check_every (see --load-check-every)
 load_high_limit (see --load-high-limit)
 load_low_limit (see --load-low-limit)
 load_watch (see --load-watch)
 log_level (see --log-level)
 log_stderr (see --log-stderr)
 log_stdout (see --log-stdout)
 naked_res (see --naked-res)
 name (see --name)
 no_screensaver (see --no-screensaver)
 on_multiple_instance (see --on-multiple-instance)
 pid_dir (see --pid-dir)
 restart (see --restart)
 show_stderr (see --no-show-stderr)
 show_stdout (see --no-show-stdout)
 single_instance (see --single-instance)
 timeout (see --timeout)

ENVIRONMENT

GOVPROC_PERICMD_OPT => str

Specify additional command-line options.

FILES

~/.config/govproc-pericmd.conf

~/govproc-pericmd.conf

/etc/govproc-pericmd.conf

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/App-govproc-pericmd.

SOURCE

Source repository is at https://github.com/perlancar/perl-App-govproc-pericmd.

BUGS

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=App-govproc-pericmd

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.

SEE ALSO

govproc

AUTHOR

perlancar <perlancar@cpan.org>

COPYRIGHT AND LICENSE

This software is copyright (c) 2019 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.