diff --git a/Base/usr/share/man/man1/pgrep.md b/Base/usr/share/man/man1/pgrep.md index 5ce4033a51..fd1d375308 100644 --- a/Base/usr/share/man/man1/pgrep.md +++ b/Base/usr/share/man/man1/pgrep.md @@ -5,7 +5,7 @@ pgrep - look up processes based on name ## Synopsis ```sh -$ pgrep [--count] [-d delimiter] [--ignore-case] [--list-name] [--newest] [--oldest] [--uid uid-list] [--invert-match] [--exact] +$ pgrep [--count] [-d delimiter] [--ignore-case] [--list-name] [--newest] [--oldest] [--older seconds] [--uid uid-list] [--invert-match] [--exact] ``` ## Options @@ -16,6 +16,7 @@ $ pgrep [--count] [-d delimiter] [--ignore-case] [--list-name] [--newest] [--old * `-l`, `--list-name`: List the process name in addition to its pid * `-n`, `--newest`: Select the most recently created process only * `-o`, `--oldest`: Select the least recently created process only +* `-O`, `--older`: Select only processes older than the specified number of seconds * `-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 * `-v`, `--invert-match`: Select non-matching lines diff --git a/Userland/Utilities/pgrep.cpp b/Userland/Utilities/pgrep.cpp index 7f06b732fa..14e70d3173 100644 --- a/Userland/Utilities/pgrep.cpp +++ b/Userland/Utilities/pgrep.cpp @@ -30,6 +30,7 @@ ErrorOr serenity_main(Main::Arguments args) bool exact_match = false; bool newest_only = false; bool oldest_only = false; + Optional display_if_older_than; HashTable uids_to_filter_by; StringView pattern; @@ -40,6 +41,23 @@ ErrorOr serenity_main(Main::Arguments args) 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(Core::ArgsParser::Option { + .argument_mode = Core::ArgsParser::OptionArgumentMode::Required, + .help_string = "Select only processes older than the specified number of seconds", + .long_name = "older", + .short_name = 'O', + .value_name = "seconds", + .accept_value = [&display_if_older_than](StringView seconds_string) { + auto number = seconds_string.to_uint(); + + if (number.has_value() && number.value() <= NumericLimits::max()) { + auto now_time = UnixDateTime::now(); + display_if_older_than = now_time - Duration::from_seconds(static_cast(number.value())); + } + + return display_if_older_than.has_value(); + }, + }); args_parser.add_option(Core::ArgsParser::Option { .argument_mode = Core::ArgsParser::OptionArgumentMode::Required, .help_string = "Select only processes whose UID is in the given comma-separated list. Login name or numerical user ID may be used", @@ -99,6 +117,9 @@ ErrorOr serenity_main(Main::Arguments args) if (!uids_to_filter_by.is_empty() && !uids_to_filter_by.contains(it.uid)) continue; + if (display_if_older_than.has_value() && it.creation_time >= display_if_older_than.value()) + continue; + matches.append(it); } }