1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-20 18:15:07 +00:00

pkill: Add -n option to kill the newest matching process only

This commit is contained in:
Tim Ledbetter 2023-06-15 18:25:05 +01:00 committed by Andreas Kling
parent 0621a83cb4
commit 519893d31f
2 changed files with 17 additions and 7 deletions

View file

@ -5,7 +5,7 @@ pkill - Signal processes based on name
## Synopsis ## Synopsis
```sh ```sh
$ pkill [--count] [--ignore-case] [--echo] [--signal signame] [--uid uid-list] [--exact] <process-name> $ pkill [--count] [--ignore-case] [--echo] [--newest] [--signal signame] [--uid uid-list] [--exact] <process-name>
``` ```
## Options ## Options
@ -13,6 +13,7 @@ $ pkill [--count] [--ignore-case] [--echo] [--signal signame] [--uid uid-list] [
* `-c`, `--count`: Display the number of matching processes * `-c`, `--count`: Display the number of matching processes
* `-i`, `--ignore-case`: Make matches case-insensitive * `-i`, `--ignore-case`: Make matches case-insensitive
* `-e`, `--echo`: Display what is killed * `-e`, `--echo`: Display what is killed
* `-n`, `--newest`: Kill the most recently created process only
* `-s signame`, `--signal signame`: Signal to send. The signal name or number may be used * `-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 * `-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

View file

@ -6,6 +6,7 @@
*/ */
#include <AK/Format.h> #include <AK/Format.h>
#include <AK/QuickSort.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibCore/Account.h> #include <LibCore/Account.h>
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
@ -29,6 +30,7 @@ ErrorOr<int> serenity_main(Main::Arguments args)
bool case_insensitive = false; bool case_insensitive = false;
bool echo = false; bool echo = false;
bool exact_match = false; bool exact_match = false;
bool newest_only = false;
StringView pattern; StringView pattern;
HashTable<uid_t> uids_to_filter_by; HashTable<uid_t> uids_to_filter_by;
int signal = SIGTERM; int signal = SIGTERM;
@ -37,6 +39,7 @@ ErrorOr<int> serenity_main(Main::Arguments args)
args_parser.add_option(display_number_of_matches, "Display the number of matching processes", "count", 'c'); args_parser.add_option(display_number_of_matches, "Display the number of matching processes", "count", 'c');
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(echo, "Display what is killed", "echo", 'e'); 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(Core::ArgsParser::Option { args_parser.add_option(Core::ArgsParser::Option {
.argument_mode = Core::ArgsParser::OptionArgumentMode::Required, .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
.help_string = "Signal number to send. A signal name or number may be used", .help_string = "Signal number to send. A signal name or number may be used",
@ -115,12 +118,18 @@ ErrorOr<int> serenity_main(Main::Arguments args)
} }
} }
for (auto& process : matched_processes) { if (!matched_processes.is_empty()) {
auto result = Core::System::kill(process.pid, signal); quick_sort(matched_processes, [](auto const& a, auto const& b) { return a.creation_time < b.creation_time; });
if (result.is_error()) if (newest_only)
warnln("Killing pid {} failed. {}", process.pid, result.release_error()); matched_processes = { matched_processes.last() };
else if (echo)
outln("{} killed (pid {})", process.name, process.pid); for (auto& process : matched_processes) {
auto result = Core::System::kill(process.pid, signal);
if (result.is_error())
warnln("Killing pid {} failed. {}", process.pid, result.release_error());
else if (echo)
outln("{} killed (pid {})", process.name, process.pid);
}
} }
if (display_number_of_matches) if (display_number_of_matches)