diff --git a/Base/usr/share/man/man1/pkill.md b/Base/usr/share/man/man1/pkill.md index ca1fae6aa4..b706480d5b 100644 --- a/Base/usr/share/man/man1/pkill.md +++ b/Base/usr/share/man/man1/pkill.md @@ -5,7 +5,7 @@ pkill - Signal processes based on name ## Synopsis ```sh -$ pkill [--count] [--ignore-case] [--echo] [--newest] [--oldest] [--signal signame] [--uid uid-list] [--exact] +$ pkill [--count] [--ignore-case] [--echo] [--newest] [--oldest] [--older seconds] [--signal signame] [--uid uid-list] [--exact] ``` ## Options @@ -15,6 +15,7 @@ $ pkill [--count] [--ignore-case] [--echo] [--newest] [--oldest] [--signal signa * `-e`, `--echo`: Display what is killed * `-n`, `--newest`: Kill 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 * `-s signame`, `--signal signame`: Signal to send. The signal name or number 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 diff --git a/Userland/Utilities/pkill.cpp b/Userland/Utilities/pkill.cpp index 23d59b6cbf..bbbd1baa98 100644 --- a/Userland/Utilities/pkill.cpp +++ b/Userland/Utilities/pkill.cpp @@ -32,6 +32,7 @@ ErrorOr serenity_main(Main::Arguments args) bool exact_match = false; bool newest_only = false; bool oldest_only = false; + Optional display_if_older_than; StringView pattern; HashTable uids_to_filter_by; int signal = SIGTERM; @@ -42,6 +43,23 @@ ErrorOr serenity_main(Main::Arguments args) args_parser.add_option(echo, "Display what is killed", "echo", 'e'); args_parser.add_option(newest_only, "Kill the most recently created process only", "newest", 'n'); args_parser.add_option(oldest_only, "Kill the least recently created process only", "oldest", 'o'); + args_parser.add_option(Core::ArgsParser::Option { + .argument_mode = Core::ArgsParser::OptionArgumentMode::Required, + .help_string = "Kill 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 = "Signal number to send. A signal name or number may be used", @@ -122,6 +140,9 @@ ErrorOr serenity_main(Main::Arguments args) if (!uids_to_filter_by.is_empty() && !uids_to_filter_by.contains(process.uid)) continue; + if (display_if_older_than.has_value() && process.creation_time >= display_if_older_than.value()) + continue; + matched_processes.append(process); } }