About nice
nice runs a command with modified scheduling priority. It's used to control how much CPU time a process gets relative to other processes. Lower nice values mean higher priority, while higher nice values mean lower priority.
Basic Syntax: nice [OPTION] [COMMAND [ARG]...]
Nice Value Range: -20 (highest priority) to +19 (lowest priority). Default is 10 when using nice without arguments.
Related Commands: renice (change priority of running process), ionice (I/O scheduling priority), top/htop (view process priorities)
| Nice Value | Priority Level | Use Case |
|---|---|---|
| -20 to -10 | Very High | Critical system processes (requires root) |
| -9 to 0 | High | Important user processes (requires root for negative values) |
| 1 to 10 | Normal | Standard processes (default is 10) |
| 11 to 19 | Low | Background tasks, batch jobs |
Example 1: Running a Command with Default Lower Priority
Use nice without arguments to run a command with a nice value of 10 (lower priority than normal).
# Run a CPU-intensive compression with lower priority
nice tar -czf backup.tar.gz /home/user/data/
# Run a long-running compile job
nice make -j4
# Check the nice value of the running process
ps -l | grep tar
Output (ps -l shows NI column):
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 R 1000 12345 12344 95 90 10 - 2345 - pts/0 00:01:23 tar
Note:
Without specifying a value, nice defaults to +10. The NI column in ps output shows the nice value. PRI shows the actual priority (PRI = 80 + NI on most systems).
Example 2: Specifying Custom Nice Values
Use the -n option to specify exact nice values for different priority levels.
# Run with nice value of 15 (very low priority)
nice -n 15 find / -name "*.log" -type f
# Alternative syntax (older form, still works)
nice -15 find / -name "*.log" -type f
# Run with nice value of 5
nice -n 5 ./my_script.sh
# Run backup with lowest priority
nice -n 19 rsync -av /source/ /destination/
Checking the priority:
$ ps -o pid,ni,cmd -p $(pgrep rsync)
PID NI CMD
23456 19 rsync -av /source/ /destination/
Note:
Regular users can only set nice values from 0 to 19. Setting negative values (higher priority) requires root privileges.
Example 3: Using Nice with Root Privileges (Negative Values)
Root can set negative nice values to increase process priority above normal.
# Run database backup with higher priority (as root)
sudo nice -n -10 mysqldump --all-databases > backup.sql
# Run critical monitoring script with highest priority
sudo nice -n -20 ./monitor_critical_service.sh
# Run time-sensitive data processing
sudo nice -n -5 python process_realtime_data.py
Verification:
$ sudo ps -o pid,ni,cmd -p $(pgrep mysqldump)
PID NI CMD
34567 -10 mysqldump --all-databases
Note:
Negative nice values give processes more CPU time. Use sparingly as they can impact system responsiveness. Values below -10 should be reserved for truly critical processes.
Example 4: Nice with Background Jobs
Combine nice with background job execution to run low-priority tasks without blocking the terminal.
# Run video encoding in background with low priority
nice -n 15 ffmpeg -i input.mp4 -c:v libx264 output.mp4 &
# Multiple background jobs with different priorities
nice -n 10 ./job1.sh &
nice -n 15 ./job2.sh &
nice -n 19 ./job3.sh &
# Check running background jobs
jobs -l
# View all with their nice values
ps -eo pid,ni,cmd | grep -E "job|ffmpeg"
Output:
[1] 45678
[2] 45679
[3] 45680
PID NI CMD
45678 10 /bin/bash ./job1.sh
45679 15 /bin/bash ./job2.sh
45680 19 /bin/bash ./job3.sh
Note:
The ampersand (&) runs the command in the background. This combination is perfect for long-running tasks that shouldn't interfere with interactive work.
Example 5: Using Nice in Scripts
Incorporate nice into shell scripts to ensure they run with appropriate priority.
#!/bin/bash
# backup_script.sh
# Run the entire script with low priority
# Method 1: Relaunch script with nice if not already niced
if [ -z "$NICED" ]; then
export NICED=1
exec nice -n 15 "$0" "$@"
fi
echo "Running backup with PID $$ and nice value:"
ps -o ni -p $$
# Backup operations
tar -czf /backup/home_$(date +%Y%m%d).tar.gz /home/
rsync -av /data/ /backup/data/
echo "Backup completed at $(date)"
# Method 2: Apply nice to specific commands only
#!/bin/bash
echo "Starting maintenance..."
nice -n 10 du -sh /var/log/* > disk_usage.txt
nice -n 15 find /tmp -type f -mtime +7 -delete
echo "Maintenance completed"
Output:
Running backup with PID 56789 and nice value:
NI
15
Backup completed at Wed Nov 12 10:30:45 EST 2025
Note:
Method 1 (exec nice) ensures the entire script runs with the specified priority. Method 2 allows fine-grained control over individual commands.
Example 6: Nice with Pipelines
Apply nice to complex command pipelines where multiple processes work together.
# Lower priority for entire pipeline
nice -n 15 sh -c "find /logs -name '*.log' | xargs grep ERROR | sort | uniq -c"
# Each command in pipeline inherits the nice value
nice -n 10 sh -c "cat large_file.txt | sed 's/old/new/g' | gzip > output.gz"
# Complex data processing pipeline
nice -n 12 sh -c "zcat access.log.gz | \
awk '{print \$1}' | \
sort | \
uniq -c | \
sort -rn | \
head -20 > top_ips.txt"
Checking pipeline processes:
$ ps -eo pid,ni,cmd | grep -E "find|grep|sort"
67890 15 find /logs -name *.log
67891 15 xargs grep ERROR
67892 15 sort
67893 15 uniq -c
Note:
Use sh -c to wrap the entire pipeline. All processes in the pipeline inherit the nice value. Without sh -c, only the first command would be affected.
Example 7: Monitoring Nice Values of Running Processes
Use various tools to check and monitor the nice values of processes.
# View nice values with ps
ps -eo pid,ni,pri,cmd | head -20
# Sort processes by nice value
ps -eo pid,ni,cmd --sort=ni
# View specific process nice value
ps -o ni -p 12345
# Using top (press 'f' then select nice field)
top -o NI
# Show only processes with specific nice values
ps -eo pid,ni,cmd | awk '$2 == 10'
# View all your processes with nice values
ps -u $USER -o pid,ni,cmd
Sample Output:
PID NI PRI CMD
1 0 80 /sbin/init
456 0 80 /usr/sbin/sshd
1234 10 90 tar -czf backup.tar.gz
5678 15 95 find / -name *.log
9012 19 99 rsync -av /source /dest
Note:
PRI (priority) = 80 + NI on most Linux systems. Lower PRI means higher actual priority. Top and htop provide interactive views with color coding.
Example 8: Nice for CPU-Intensive Batch Processing
Use nice for batch jobs that shouldn't interfere with interactive system performance.
# Image batch processing with minimal system impact
nice -n 18 for img in *.jpg; do
convert "$img" -resize 50% "thumb_$img"
done
# Scientific computation in background
nice -n 19 python heavy_calculation.py &
# Multiple video conversions
for video in *.avi; do
nice -n 15 ffmpeg -i "$video" "${video%.avi}.mp4" &
done
wait # Wait for all background jobs to complete
# Database maintenance during business hours
nice -n 12 pg_dump mydatabase > backup.sql
System Impact:
# Before nice (high CPU usage blocks system):
load average: 8.45, 7.23, 6.89
# After nice -n 19 (system remains responsive):
load average: 1.23, 1.45, 1.67
Note:
Nice values 15-19 are ideal for batch processing. The scheduler will give CPU time to interactive processes first, keeping the system responsive.
Example 9: Nice with Cron Jobs
Apply nice to cron jobs to prevent scheduled tasks from impacting system performance.
# Edit crontab
crontab -e
# Run nightly backup with low priority
0 2 * * * nice -n 15 /usr/local/bin/backup.sh
# Daily log rotation with minimal impact
0 0 * * * nice -n 19 /usr/sbin/logrotate /etc/logrotate.conf
# Hourly cleanup with moderate priority
0 * * * * nice -n 10 /usr/local/bin/cleanup_temp.sh
# Weekly report generation
0 3 * * 0 nice -n 18 /opt/scripts/generate_reports.sh
# Database optimization during off-hours
30 3 * * * nice -n 12 mysqlcheck --optimize --all-databases
Cron Log:
Nov 12 02:00:01 server CRON[12345]: (root) CMD (nice -n 15 /usr/local/bin/backup.sh)
Nov 12 03:00:01 server CRON[12567]: (root) CMD (nice -n 18 /opt/scripts/generate_reports.sh)
Note:
Cron jobs run at nice value 0 by default. Adding nice ensures they don't impact system performance. Essential for maintenance tasks during business hours.
Example 10: Combining Nice with Other Process Control Tools
Use nice alongside other tools like ionice, taskset, and cpulimit for comprehensive process control.
# Combine nice (CPU priority) with ionice (I/O priority)
nice -n 15 ionice -c 3 rsync -av /source/ /dest/
# Use with taskset to limit CPU cores
nice -n 10 taskset -c 0,1 ./compute_intensive_app
# Combine with timeout to limit execution time
nice -n 15 timeout 1h ./long_running_process
# Use with cpulimit to cap CPU usage percentage
nice -n 15 cpulimit -l 50 ./cpu_hog &
# Full resource control example
# CPU priority (nice), I/O priority (ionice), CPU cores (taskset)
nice -n 18 ionice -c 2 -n 7 taskset -c 0-3 \
tar -czf huge_backup.tar.gz /data/
# Monitor the controlled process
ps -o pid,ni,cmd -p $(pgrep tar)
ionice -p $(pgrep tar)
Output:
PID NI CMD
89012 18 tar -czf huge_backup.tar.gz /data/
best-effort: prio 7
Note:
Combining tools provides fine-grained resource control: nice (CPU scheduling), ionice (I/O scheduling), taskset (CPU affinity), cpulimit (CPU percentage cap). Essential for multi-tenant systems.
Additional Tips & Best Practices
- Default behavior: Without -n flag, nice defaults to +10. Command:
nice command=nice -n 10 command - renice command: Change nice value of running process:
renice -n 15 -p PID - Permission requirements: Regular users can only increase nice values (lower priority). Root needed for negative values
- Inheritance: Child processes inherit the nice value of their parent
- Scheduler considerations: Nice values affect CPU scheduling but don't guarantee exact CPU time allocation
- Real-time priority: For real-time requirements, consider chrt command instead of nice
- System load: Nice is most effective under high load. On idle systems, all processes get CPU time regardless
- I/O operations: Nice affects CPU priority, not I/O. Use ionice for I/O-bound processes
- Monitoring: Use top, htop, or ps to verify nice values are being applied correctly
- Best practices: Values 10-15 for routine background jobs, 15-19 for low-priority batch processing, 0-9 for important tasks, negative values only for critical processes
- systemd integration: Set nice values in service files with:
Nice=15 - Docker/containers: Use --cpu-shares for container priority instead of nice in most cases