Skip to content

Instantly share code, notes, and snippets.

@anton-chernianu
Created June 22, 2023 09:56
Show Gist options
  • Save anton-chernianu/072528f9e9b6abd137a489d79a6e1862 to your computer and use it in GitHub Desktop.
Save anton-chernianu/072528f9e9b6abd137a489d79a6e1862 to your computer and use it in GitHub Desktop.
Launch cpulimit for all node js processes
#!/bin/bash
CPULIMIT_PERCENT=20
# Launch cpulimit in the background for the specified Node process
limit_node_process() {
local pid=$1
cpulimit -p "$pid" -l "$CPULIMIT_PERCENT" &
}
# Monitor new processes
monitor_node_processes() {
while true; do
# Get all Node process ids
local node_pids=($(pgrep -f "node"))
for pid in "${node_pids[@]}"; do
# Check if cpulimit is already running for the process
if ! pgrep -f "cpulimit -p $pid" >/dev/null; then
# Launch cpulimit for the new process
limit_node_process "$pid"
fi
done
sleep 1 # Monitoring interval
done
}
# Start monitoring new processes in the background
monitor_node_processes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment