mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 01:41:59 -05:00
pkill: Add -o
option to kill the oldest matching process only
This commit is contained in:
parent
519893d31f
commit
aa79a4ed9a
2 changed files with 12 additions and 1 deletions
|
@ -5,7 +5,7 @@ pkill - Signal processes based on name
|
|||
## Synopsis
|
||||
|
||||
```sh
|
||||
$ pkill [--count] [--ignore-case] [--echo] [--newest] [--signal signame] [--uid uid-list] [--exact] <process-name>
|
||||
$ pkill [--count] [--ignore-case] [--echo] [--newest] [--oldest] [--signal signame] [--uid uid-list] [--exact] <process-name>
|
||||
```
|
||||
|
||||
## Options
|
||||
|
@ -14,6 +14,7 @@ $ pkill [--count] [--ignore-case] [--echo] [--newest] [--signal signame] [--uid
|
|||
* `-i`, `--ignore-case`: Make matches case-insensitive
|
||||
* `-e`, `--echo`: Display what is killed
|
||||
* `-n`, `--newest`: Kill the most recently created process only
|
||||
* `-o`, `--oldest`: Select the least recently created process only
|
||||
* `-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
|
||||
|
|
|
@ -31,6 +31,7 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
|||
bool echo = false;
|
||||
bool exact_match = false;
|
||||
bool newest_only = false;
|
||||
bool oldest_only = false;
|
||||
StringView pattern;
|
||||
HashTable<uid_t> uids_to_filter_by;
|
||||
int signal = SIGTERM;
|
||||
|
@ -40,6 +41,7 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
|||
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(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 = "Signal number to send. A signal name or number may be used",
|
||||
|
@ -89,6 +91,12 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
|||
args_parser.add_positional_argument(pattern, "Process name to search for", "process-name");
|
||||
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;
|
||||
}
|
||||
|
||||
auto all_processes = TRY(Core::ProcessStatisticsReader::get_all());
|
||||
|
||||
PosixOptions options {};
|
||||
|
@ -122,6 +130,8 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
|||
quick_sort(matched_processes, [](auto const& a, auto const& b) { return a.creation_time < b.creation_time; });
|
||||
if (newest_only)
|
||||
matched_processes = { matched_processes.last() };
|
||||
else if (oldest_only)
|
||||
matched_processes = { matched_processes.first() };
|
||||
|
||||
for (auto& process : matched_processes) {
|
||||
auto result = Core::System::kill(process.pid, signal);
|
||||
|
|
Loading…
Reference in a new issue