mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 08:54:58 +00:00
pkill: Add -O
option to filter processes by age
This option allows the user to specify a number of seconds. Only processes older than the given number of seconds are killed.
This commit is contained in:
parent
aa79a4ed9a
commit
17fe2c4822
2 changed files with 23 additions and 1 deletions
|
@ -32,6 +32,7 @@ ErrorOr<int> serenity_main(Main::Arguments args)
|
|||
bool exact_match = false;
|
||||
bool newest_only = false;
|
||||
bool oldest_only = false;
|
||||
Optional<UnixDateTime> display_if_older_than;
|
||||
StringView pattern;
|
||||
HashTable<uid_t> uids_to_filter_by;
|
||||
int signal = SIGTERM;
|
||||
|
@ -42,6 +43,23 @@ ErrorOr<int> 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<u64>();
|
||||
|
||||
if (number.has_value() && number.value() <= NumericLimits<i64>::max()) {
|
||||
auto now_time = UnixDateTime::now();
|
||||
display_if_older_than = now_time - Duration::from_seconds(static_cast<i64>(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<int> 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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue