On Debian (or any Linux system) without sudo or root access, you can use cgroups (Control Groups) to monitor and trigger actions based on memory usage without polling. However, creating and managing cgroups typically requires root privileges.
systemd User Service with Memory LimitIf your user has a systemd user instance (common on modern Debian), you can create a user service that runs your command when memory usage exceeds a threshold. This approach uses systemd's built-in memory monitoring and does not require polling.
Create a user service file (e.g., ~/.config/systemd/user/memory-watch.service):
ini[Unit] Description=Run command when memory usage exceeds threshold [Service] Type=oneshot ExecStart=/path/to/your/command MemoryHigh=80% # Adjust threshold as needed MemoryMax=90% # Hard limit (optional)
MemoryHigh: Triggers throttling or actions when memory usage exceeds this percentage.MemoryMax: Hard limit (optional, but useful for testing).Create a companion .path or .timer unit (if you want to trigger on specific events or intervals, but this is not polling).
Enable and start the service:
bashsystemctl --user enable memory-watch.service systemctl --user start memory-watch.service
inotify + /proc (No Polling)If you cannot use systemd, you can use inotify to monitor /proc/self/status or /proc/meminfo for changes. However, this is not event-driven for memory thresholds—it only detects file changes, not specific values.
inotifywait):bashwhile inotifywait -q -e modify /proc/meminfo; do # Parse /proc/meminfo for MemAvailable or your metric mem_available=$(grep MemAvailable /proc/meminfo | awk '{print $2}') if [ "$mem_available" -lt YOUR_THRESHOLD ]; then /path/to/your/command fi done
/proc/meminfo on every change, which is not true event-driven but avoids active polling loops.systemd User Servicesystemd user instance (enabled by default on Debian with loginctl enable-linger).systemd user instance:bashloginctl show-user $(whoami) | grep Linger
If Linger=no, enable it:
bashloginctl enable-linger $(whoami)
Use systemd user service with MemoryHigh if available. It is the only non-polling, non-root solution for this use case on Debian. If systemd is not an option, you must use a lightweight polling script (e.g., watch or cron).
Vibe can make mistakes. Check answers. Learn more