Linux Command Reference

renice

Synopsis

renice [-n] priority [[-p] pid...] [[-g] pgrp...] [[-u] user...]

Description

The renice command alters the scheduling priority of one or more running processes. Process priority determines how much CPU time a process receives relative to other processes. The kernel scheduler uses nice values (ranging from -20 to 19) to determine process priority, where lower values indicate higher priority and more CPU time allocation.

This is crucial for system administrators managing resource-intensive applications, ensuring critical processes get adequate CPU time, or throttling background tasks to prevent system performance degradation. Unlike the nice command which sets priority at process launch, renice modifies the priority of already-running processes.

Key Concepts

Detailed Examples

Example 1

Increase Priority of a Single Process (Root Required)

Decrease the nice value (increase priority) of a CPU-intensive compilation process to ensure it completes faster. This requires root privileges since we're decreasing the nice value.
# First, identify the process ps aux | grep "gcc" # Output shows: user 12345 ... gcc -O3 large_project.c # Increase priority by setting nice to -5 (higher priority) sudo renice -n -5 -p 12345
Output:
12345 (process ID) old priority 0, new priority -5
Note:

The -n flag specifies the new nice value. A nice value of -5 gives this process significantly more CPU time than processes at the default nice value of 0.

Example 2

Decrease Priority of Background Task (User Can Do This)

Lower the priority of a long-running backup script to prevent it from interfering with interactive applications. Regular users can increase nice values for their own processes.
# Check current priority ps -o pid,ni,cmd -p 23456 # Output: PID NI CMD # 23456 0 /bin/bash backup.sh # Decrease priority by setting nice to 15 renice -n 15 -p 23456
Output:
23456 (process ID) old priority 0, new priority 15
Note:

With nice value 15, the backup process will receive less CPU time, allowing interactive applications to remain responsive. This is commonly used for batch processing jobs.

Example 3

Adjust Priority for All Processes of a User

Lower the priority of all processes owned by a specific user who is running resource-intensive data analysis jobs. This prevents one user from monopolizing system resources.
# View current processes for user 'dataanalyst' ps -u dataanalyst -o pid,ni,cmd # Lower priority for ALL processes owned by this user sudo renice -n 10 -u dataanalyst
Output:
1001 (user ID) old priority 0, new priority 10 2341: old priority 0, new priority 10 2342: old priority 0, new priority 10 2343: old priority 5, new priority 10 ...
Warning:

This affects ALL processes for the user, including their shell session and any interactive applications. Use with caution.

Example 4

Adjust Priority by Process Group ID

Modify priority for an entire process group. This is useful when a parent process has spawned multiple child processes that should all have the same priority adjustment.
# Identify process group ID (PGID) ps -eo pid,pgid,ni,cmd | grep render # Output shows multiple processes with PGID 5678 # Adjust priority for entire process group sudo renice -n 5 -g 5678
Output:
5678 (process group ID) old priority 0, new priority 5
Note:

Process groups are typically created when a process forks children. All processes in the group share the same PGID, making it easy to manage them collectively.

Example 5

Multiple Process IDs in One Command

Adjust priority for several specific processes simultaneously. Useful when you have multiple related but not grouped processes that need the same priority adjustment.
# Check current priorities ps -o pid,ni,cmd -p 1234,2345,3456 # Set all to nice value 8 sudo renice -n 8 -p 1234 -p 2345 -p 3456 # Shorter syntax without repeating -p flag sudo renice -n 8 1234 2345 3456
Output:
1234 (process ID) old priority 0, new priority 8 2345 (process ID) old priority 5, new priority 8 3456 (process ID) old priority 0, new priority 8
Note:

You can specify multiple PIDs with or without repeating the -p flag. Both syntaxes are valid.

Example 6

Maximize Priority for Critical Database Process

Set maximum priority (nice value -20) for a critical database process that requires guaranteed CPU time for transaction processing. This ensures the database gets CPU time even under heavy system load.
# Identify database process pgrep -a postgres | grep "main process" # Output: 4567 postgres: main process # Set to maximum priority sudo renice -n -20 -p 4567 # Verify the change ps -o pid,ni,pri,cmd -p 4567
Output:
4567 (process ID) old priority 0, new priority -20 PID NI PRI CMD 4567 -20 60 postgres: main process
Warning:

Use maximum priority (-20) sparingly and only for truly critical processes. A process with nice -20 can starve other processes of CPU time, potentially making the system unresponsive.

Example 7

Throttle Resource-Intensive Video Encoding

Significantly lower the priority of a video encoding job that will run for hours. This ensures the encoding happens in the background without impacting system responsiveness.
# Find the ffmpeg encoding process ps aux | grep ffmpeg # Output: user 8901 ... ffmpeg -i input.mp4 -c:v h264 output.mp4 # Set to very low priority (highest nice value) renice -n 19 -p 8901 # Monitor CPU usage to verify reduced priority top -p 8901
Output:
8901 (process ID) old priority 0, new priority 19 # In top output, you'll see the PR (priority) column reflects the change: PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 8901 user 39 19 892516 123456 4567 R 15.0 2.3 5:23.45 ffmpeg
Note:

With nice value 19, the process will only get CPU time when higher-priority processes are idle. Perfect for long-running background tasks that aren't time-sensitive.

Example 8

Relative Priority Adjustment

While renice typically sets absolute nice values, you can calculate relative adjustments by first checking the current value. This example shows how to increase a process's current nice value by 5 points.
# Check current nice value CURRENT_NICE=$(ps -o ni= -p 5432) echo "Current nice: $CURRENT_NICE" # Calculate new nice value (increase by 5) NEW_NICE=$((CURRENT_NICE + 5)) # Apply new nice value renice -n $NEW_NICE -p 5432 # Alternative: Use renice increment (if supported on your system) # Some implementations support: renice +5 -p 5432
Output:
Current nice: 3 5432 (process ID) old priority 3, new priority 8
Note:

The standard renice command sets absolute values, but you can script relative adjustments. Some Unix variants support the +/- syntax for relative adjustments (e.g., renice +5 -p PID).

Example 9

Emergency Priority Boost for Hung Process Recovery

When a system monitoring process appears to be starved of CPU time and isn't responding, boost its priority to give it a chance to recover and report status before killing it.
# Check if monitoring daemon is responsive pgrep -a nagios # Output: 6789 /usr/sbin/nagios -d /etc/nagios/nagios.cfg # Check current load and nice value ps -o pid,ni,pcpu,pmem,cmd -p 6789 # Shows: 6789 0 0.1 0.5 /usr/sbin/nagios # Temporarily boost priority to recover sudo renice -n -10 -p 6789 # Give it 30 seconds, then check if it's responding sleep 30 systemctl status nagios # If recovered, return to normal priority sudo renice -n 0 -p 6789
Output:
6789 (process ID) old priority 0, new priority -10 # After 30 seconds and status check: ● nagios.service - Nagios Core Active: active (running) 6789 (process ID) old priority -10, new priority 0
Warning:

This is an emergency technique. Don't leave critical services at abnormal priorities long-term. Investigate why the process was starved in the first place.

Example 10

Script for Automated Priority Management

Create a script that automatically adjusts priorities for a batch of nightly backup processes. This demonstrates practical automation of renice in production environments.
#!/bin/bash # File: manage_backup_priority.sh # Purpose: Adjust priority for all backup processes # Configuration BACKUP_USER="backup" LOW_PRIORITY=15 NORMAL_PRIORITY=0 # Function to lower priority for backups start_low_priority_backup() { echo "Lowering priority for backup processes..." sudo renice -n $LOW_PRIORITY -u $BACKUP_USER echo "Backup processes now running at nice level $LOW_PRIORITY" } # Function to restore normal priority restore_priority() { echo "Restoring normal priority for backup processes..." sudo renice -n $NORMAL_PRIORITY -u $BACKUP_USER echo "Backup processes restored to nice level $NORMAL_PRIORITY" } # Monitor and report report_status() { echo "Current backup process priorities:" ps -u $BACKUP_USER -o pid,ni,cmd --sort=-pcpu | head -10 } # Main execution case "$1" in start) start_low_priority_backup ;; stop) restore_priority ;; status) report_status ;; *) echo "Usage: $0 {start|stop|status}" exit 1 ;; esac
Output when running: ./manage_backup_priority.sh start
Lowering priority for backup processes... 1001 (user ID) old priority 0, new priority 15 Backup processes now running at nice level 15 # Output when running: ./manage_backup_priority.sh status Current backup process priorities: PID NI CMD 9012 15 rsync -av /data /backup 9013 15 tar czf backup.tar.gz /home 9014 15 mysqldump --all-databases
Note:

This script demonstrates production-ready priority management. Schedule it with cron to automatically lower backup priority during business hours and restore it during off-hours.

Tips & Best Practices

Monitor the Impact
After changing process priority, use top, htop, or ps to verify the change took effect and monitor CPU usage. The PR (priority) column in top shows the calculated priority (20 + nice value).
Understand the Nice-to-Priority Mapping
The kernel priority (PR) is calculated as 20 + nice value. So nice -20 gives PR=0 (highest), nice 0 gives PR=20 (normal), and nice 19 gives PR=39 (lowest). Lower PR values get more CPU time.
Use Process Names with pgrep
Combine renice with pgrep to target processes by name: sudo renice -n 5 -p $(pgrep firefox). This adjusts all Firefox processes at once.
Document Your Changes
In production environments, document why you're changing process priorities. Nice values aren't persistent across reboots, so you may need to reapply them or add them to startup scripts.
Consider cgroups for Persistent Control
For persistent CPU allocation control, consider using cgroups (Control Groups) instead of renice. Cgroups provide more sophisticated resource management and persist across process restarts.
Beware of Starvation
Setting too many processes to high priority can starve normal-priority processes. The Linux scheduler is fair, but processes at nice -20 will always get CPU before nice 0 processes.
Test in Non-Production First
Before applying priority changes to production systems, test the impact in a development environment. Incorrect priority settings can cause system instability or application failures.