nice - Linux


Overview

The nice command in Linux is used to set the scheduling priority of a program. It adjusts the “niceness” value of a process, which affects its priority in the process scheduling algorithm. Lower niceness values correspond to higher scheduling priority, enabling crucial tasks to run more smoothly even when system resources are under high demand. This command is particularly useful in a multi-user environment or when a system is running multiple intensive tasks.

Syntax

The basic syntax for using nice is as follows:

nice [OPTION] [COMMAND [ARG]...]

If COMMAND is not specified, nice prints the current niceness. COMMAND must be a valid shell command with optional arguments.

Options/Flags

  • -n, --adjustment=NICE: Set the niceness to an integer between -20 (highest priority) and 19 (lowest priority). The default adjustment is 10 if no value is specified.
  • --help: Display a help message and exit.
  • --version: Output version information and exit.

Note: Only users with root privileges can set a negative nice value.

Examples

  1. Check default niceness:

    nice
    

    This command will show the current niceness of the shell if no command is specified.

  2. Increase the niceness:

    nice -n 15 find / -name "example.txt"
    

    This command runs find with a low priority, making it less likely to impact other system processes.

  3. Running a script with high priority:

    sudo nice -n -5 ./heavy_script.sh
    

    Runs heavy_script.sh with high priority (lower niceness), useful for important tasks that need more CPU allocation.

Common Issues

  • Permission Denied: Trying to set a negative niceness without root privileges will result in permission errors.
  • Invalid Niceness Values: Specifying a niceness outside the valid range (-20 to 19) gives an error. Ensure values are within this range.
  • Impact on Real-time Processes: Adjusting nice values may not significantly impact processes that are already running with real-time priorities.

Integration

Combining nice with other system commands can effectively manage system resources:

nice -n 10 tar czf backup.tar.gz /home/user & find / -name "data.csv" | nice -n 19 xargs gzip

This runs a backup creation in the background with lower priority while concurrently searching and compressing files with the lowest priority.

  • renice: Adjusts the niceness of an already running process.
  • top: Shows running processes along with their niceness values, useful for monitoring changes made by nice.

To learn more about the nice command and its nuances, refer to the Linux man pages (type man nice in the terminal) or visit the official GNU documentation online.