mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:17:45 +00:00
pgrep: Add -o
option to display the oldest matching process only
This commit is contained in:
parent
47b530fa71
commit
d03aaddb55
2 changed files with 12 additions and 1 deletions
|
@ -5,7 +5,7 @@ pgrep - look up processes based on name
|
||||||
## Synopsis
|
## Synopsis
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
$ pgrep [--count] [-d delimiter] [--ignore-case] [--list-name] [--newest] [--uid uid-list] [--invert-match] [--exact] <process-name>
|
$ pgrep [--count] [-d delimiter] [--ignore-case] [--list-name] [--newest] [--oldest] [--uid uid-list] [--invert-match] [--exact] <process-name>
|
||||||
```
|
```
|
||||||
|
|
||||||
## Options
|
## Options
|
||||||
|
@ -15,6 +15,7 @@ $ pgrep [--count] [-d delimiter] [--ignore-case] [--list-name] [--newest] [--uid
|
||||||
* `-i`, `--ignore-case`: Make matches case-insensitive
|
* `-i`, `--ignore-case`: Make matches case-insensitive
|
||||||
* `-l`, `--list-name`: List the process name in addition to its pid
|
* `-l`, `--list-name`: List the process name in addition to its pid
|
||||||
* `-n`, `--newest`: Select the most recently created process only
|
* `-n`, `--newest`: Select the most recently created process only
|
||||||
|
* `-o`, `--oldest`: Select the least recently created process only
|
||||||
* `-U uid-list`, `--uid uid-list`: Select only processes whose UID is in the given comma-separated list. Login name or numerical user ID may be used
|
* `-U uid-list`, `--uid uid-list`: Select only processes whose UID is in the given comma-separated list. Login name or numerical user ID may be used
|
||||||
* `-x`, `--exact`: Select only processes whose names match the given pattern exactly
|
* `-x`, `--exact`: Select only processes whose names match the given pattern exactly
|
||||||
* `-v`, `--invert-match`: Select non-matching lines
|
* `-v`, `--invert-match`: Select non-matching lines
|
||||||
|
|
|
@ -29,6 +29,7 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
||||||
bool invert_match = false;
|
bool invert_match = false;
|
||||||
bool exact_match = false;
|
bool exact_match = false;
|
||||||
bool newest_only = false;
|
bool newest_only = false;
|
||||||
|
bool oldest_only = false;
|
||||||
HashTable<uid_t> uids_to_filter_by;
|
HashTable<uid_t> uids_to_filter_by;
|
||||||
StringView pattern;
|
StringView pattern;
|
||||||
|
|
||||||
|
@ -37,6 +38,7 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
||||||
args_parser.add_option(pid_delimiter, "Set the string used to delimit multiple pids", "delimiter", 'd', nullptr);
|
args_parser.add_option(pid_delimiter, "Set the string used to delimit multiple pids", "delimiter", 'd', nullptr);
|
||||||
args_parser.add_option(case_insensitive, "Make matches case-insensitive", "ignore-case", 'i');
|
args_parser.add_option(case_insensitive, "Make matches case-insensitive", "ignore-case", 'i');
|
||||||
args_parser.add_option(newest_only, "Select the most recently created process only", "newest", 'n');
|
args_parser.add_option(newest_only, "Select the most recently created process only", "newest", 'n');
|
||||||
|
args_parser.add_option(oldest_only, "Select the least recently created process only", "oldest", 'o');
|
||||||
args_parser.add_option(list_process_name, "List the process name in addition to its pid", "list-name", 'l');
|
args_parser.add_option(list_process_name, "List the process name in addition to its pid", "list-name", 'l');
|
||||||
args_parser.add_option(Core::ArgsParser::Option {
|
args_parser.add_option(Core::ArgsParser::Option {
|
||||||
.argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
|
.argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
|
||||||
|
@ -67,6 +69,12 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
||||||
args_parser.add_positional_argument(pattern, "Process name to search for", "process-name");
|
args_parser.add_positional_argument(pattern, "Process name to search for", "process-name");
|
||||||
args_parser.parse(args);
|
args_parser.parse(args);
|
||||||
|
|
||||||
|
if (newest_only && oldest_only) {
|
||||||
|
warnln("The -n and -o options are mutually exclusive");
|
||||||
|
args_parser.print_usage(stderr, args.strings[0]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
PosixOptions options {};
|
PosixOptions options {};
|
||||||
if (case_insensitive)
|
if (case_insensitive)
|
||||||
options |= PosixFlags::Insensitive;
|
options |= PosixFlags::Insensitive;
|
||||||
|
@ -101,6 +109,8 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
||||||
quick_sort(matches, [](auto const& a, auto const& b) { return a.creation_time < b.creation_time; });
|
quick_sort(matches, [](auto const& a, auto const& b) { return a.creation_time < b.creation_time; });
|
||||||
if (newest_only)
|
if (newest_only)
|
||||||
matches = { matches.last() };
|
matches = { matches.last() };
|
||||||
|
else if (oldest_only)
|
||||||
|
matches = { matches.first() };
|
||||||
|
|
||||||
auto displayed_at_least_one = false;
|
auto displayed_at_least_one = false;
|
||||||
for (auto& match : matches) {
|
for (auto& match : matches) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue