This release is a pre-release and may not be stable for production use.
About
Define rules which run commands on your computer when configurable conditions match, for example when a process is gone, a window is open or it is a certain time of day.
PowerRules started as a tool to shut down, reboot or suspend a computer at the right moment. Since actions are plain shell commands, it can now run any program or script.
Rules are evaluated from top to bottom. The first matching rule runs its actions in order.
Getting Started
1. Install via pip (Python 3.11+) or download a standalone binary:
pip install powerrules
2. Create a policy file:
# yaml-language-server: $schema=https://raw.githubusercontent.com/LeoTN/PowerRules/main/assets/schema/powerrules_policy.schema.json
rules:
# Shut down when no backup process is running and the current time is between 23:00 and 01:30
- name: "Shutdown after nightly backup"
enabled: true
conditions:
and:
- process:
name: "backup.exe"
exists: false
- datetime:
between:
start: "23"
end: "1:30"
actions:
# Windows command
- name: "Shutdown"
run: "shutdown /s /t 0"
3. Validate and evaluate the policy once. Use --dry-run to see what would happen:
pwru policy validate --policy my-policy.yaml
pwru policy run --once --policy my-policy.yaml --dry-run
Features
Process & Window Matching
Match rules based on processes and window titles.
# Match if process "firefox.exe" is running
- process:
name: "firefox.exe"
exists: true
# Match if window with exact title "Firefox" exists
- window:
title: "Firefox"
exists: true
Options reference
| Option | Applies to | Default | Description |
|---|---|---|---|
name |
process |
required | Name of the process to check. |
title |
window |
required | Title of the window to check. |
exists |
both | required | Whether the process or window is expected to exist. |
match |
both | exact, case-sensitive | How the name or title is matched, see Regex Matching. |
Regex Matching
Match process names and window titles using regular expressions with full-string matching.
# Match if process name contains "fire"
- process:
name: '.*fire.*'
exists: true
match:
type: regex
case_sensitive: true # This is the default behavior
# Match if window title starts with "firefox" (case insensitive)
- window:
title: "Firefox.*"
exists: true
match:
type: regex
case_sensitive: false
Options reference
Options of match:
| Option | Default | Description |
|---|---|---|
type |
exact |
Matching mode: exact or regex (full-string match). |
case_sensitive |
true |
Whether uppercase and lowercase letters should be treated differently. |
Time-based Conditions
Match specific dates, time ranges, weekdays and months. If several of between, weekday and month are configured, all of them must match. The start of a range is inclusive, the end is exclusive.
# Match every day from 23:00 until 1:30 the next morning
- datetime:
between:
start: "23"
end: "1:30"
# Match on Mondays and Fridays
- datetime:
weekday:
- "Monday"
- "Friday"
# Match in June, July and August
- datetime:
month:
- "June"
- "July"
- "August"
# Match on the whole days 2026-08-21 and 2026-08-22 (the end date 2026-08-23 is not included)
- datetime:
between:
start: "2026-08-21"
end: "2026-08-23"
# Match from 2026-08-21 18:00 until 2026-08-23 6:00
- datetime:
between:
start: "2026-08-21 18:00"
end: "2026-08-23 6:00"
# Match on Saturdays and Sundays from 2026-08-21 until the end of 2026
- datetime:
between:
start: "2026-08-21"
end: "2027-01-01"
weekday:
- "Saturday"
- "Sunday"
# Match on Saturday and Sunday nights in December from 22:00 until 6:00 the next morning
- datetime:
between:
start: "22"
end: "6"
weekday:
- "Saturday"
- "Sunday"
month:
- "December"
All values use the local time of the computer, timezones are not supported.
Options reference
At least one of between, weekday and month is required.
| Option | Description |
|---|---|
between |
Range during which the condition is satisfied. Both boundaries must be of the same kind (see below). |
weekday |
List of English weekday names (case-insensitive), e.g. Monday. |
month |
List of English month names (case-insensitive), e.g. January. |
weekday and month: If the time range crosses midnight, they refer to the day on which the range starts. For absolute ranges, they refer to the current day.
Kinds of between:
| Kind | Format | Example | Description |
|---|---|---|---|
| Time of day | H, HH, H:MM, HH:MM, H:MM:SS or HH:MM:SS |
"23", "1:30" |
Repeats every day and may cross midnight. |
| Date | YYYY-MM-DD |
"2026-08-21" |
Covers whole days, from 0:00 of the start date until 0:00 of the end date. |
| Date with time | YYYY-MM-DD followed by a space or T and a time |
"2026-08-21 18:00" |
Absolute range without timezone. |
Logical Conditions
Combine multiple conditions using and, or, and not.
# Match if (condition_1 OR condition_2) AND NOT condition_3
- and:
- or:
- condition_1: ...
- condition_2: ...
- not:
condition_3: ...
Actions
When a rule matches, its actions run one after another as commands in a shell.
actions:
# A failing backup does not prevent the remaining actions
- name: "Backup"
run: "backup.sh"
shell: bash
working_directory: scripts # Relative to the policy file
env:
TARGET: "/mnt/backup"
timeout: 600
success_exit_codes: [0, 3]
continue_on_error: true
# Start a command in the background without waiting for it
- run: "notify.sh"
shell: bash
wait: false
# Shutting down may require additional permissions on Linux
- name: "Shutdown"
run: "systemctl poweroff"
Options reference
| Option | Default | Description |
|---|---|---|
run |
required | Command to run. A command with several lines stops at the first failing line. |
name |
Action N of M |
Name of the action, used in log messages. |
shell |
powershell on Windows, sh on Linux and macOS |
One of sh, bash, cmd, powershell or pwsh. The shell has to be installed. |
working_directory |
directory of the policy file | Relative paths are resolved against the directory of the policy file. |
env |
none | Additional environment variables. Numbers and booleans are converted to strings. Names starting with POWERRULES_ are reserved. |
timeout |
60 |
Maximum run time in seconds. Afterwards, the command and its child processes are terminated. null disables the limit. |
success_exit_codes |
[0] |
Exit codes which count as success. |
wait |
true |
false starts the command in the background. Cannot be combined with timeout or success_exit_codes. |
continue_on_error |
false |
Whether the remaining actions still run if this action fails. |
Commands can read the name of their rule and action from the environment variables POWERRULES_RULE_NAME and POWERRULES_ACTION_NAME.
If an action fails (unexpected exit code, timeout or the command cannot be started), the remaining actions are skipped unless continue_on_error is set.
Policy Evaluation
Evaluate a policy with pwru policy run. Without --once, the rules are evaluated every 10 seconds until PowerRules is stopped. A matching rule only runs its actions when it becomes the active match, not on every evaluation while it keeps matching.
# Evaluate continuously
pwru policy run
# Evaluate once and exit
pwru policy run --once
# Only report which rule would match without running any commands
pwru policy run --dry-run
The policy is loaded once at startup, so changes to the file require a restart. If a condition cannot be evaluated or an action fails, the evaluation stops.
Options reference
| Option | Applies to | Default | Description |
|---|---|---|---|
--policy, -p |
all policy commands |
powerrules.yaml |
Path to the PowerRules policy file. |
--once |
policy run |
off | Evaluate the policy once and then exit. |
--stop-on-match |
policy run |
off | Keep evaluating until the first rule matches, then stop. |
--dry-run |
policy run |
off | Evaluate the policy without executing any matching action. |
Logging
All policy commands accept the following logging options:
pwru policy run --log-level DEBUG --log-file my-log.log
Messages are shown on the console and written to a rotating log file. Commands of actions and their output are only logged on DEBUG level. The error output of a failed action is always logged as an error.
Options reference
| Option | Default | Description |
|---|---|---|
--log-level |
INFO |
Minimum log level shown on the console and written to the log file. One of DEBUG, INFO, WARNING or ERROR. |
--log-file |
powerrules.log |
Path to the rotating log file. |
--log-level-file |
value of --log-level |
Minimum log level written to the log file. |
Supported Platforms
| Platform | Standalone Binary | Notes |
|---|---|---|
| Windows 10/11 | powerrules-windows-x86_64.exe | Window conditions only work when PowerRules runs in the logged-in user's session. |
| Linux | powerrules-linux-x86_64 powerrules-linux-arm64 |
Window conditions require an X11 session (Wayland is currently not supported). |
| macOS | powerrules-macos-arm64 | Window conditions require the screen recording permission. |
Platform independent via pip:
pip install powerrules
Credits & License
- Inkscape → program used to design the logo
- Nuitka → standalone binaries
- psutil → process information
- Pydantic → configuration validation
- PyYAML → YAML policy parsing
- rich → console output formatting
- Typer → command-line interface
This repository is licensed under the MIT License.
Metadata
Release files for powerrules 0.6.0b2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| powerrules-0.6.0b2.tar.gz | 39.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| powerrules-0.6.0b2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 86.8 kB
Release files / powerrules-0.6.0b2.tar.gz
| Download URL | powerrules-0.6.0b2.tar.gz |
|---|---|
| Size | 39.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5667f3a5cb2be78dbf20fa1f5ae048fea9955ddb0c0ea33101cec5fd55566578
|
|
BLAKE2b-256 checksum How to use checksums |
2c903d56d4406e474e5f210580d6a02b284ee7bceee3c115c460ea88ab6d922d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.4.1 CPython/3.12.3 Linux/6.17.0-1022-azure
|
Release files / powerrules-0.6.0b2-py3-none-any.whl
| Download URL | powerrules-0.6.0b2-py3-none-any.whl |
|---|---|
| Size | 47.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
54f5cbdc43d6efa657520679f4763f29602ffe5f926f456b09ea6d3f2a9c8aa5
|
|
BLAKE2b-256 checksum How to use checksums |
e96e6c7fbed5783037e697822f47feb6b04a09d2c4ac0612a43c8a54b901732e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.4.1 CPython/3.12.3 Linux/6.17.0-1022-azure
|