nice

process managementLinux/Unix
The nice command is one of the most frequently used commands in Linux/Unix-like operating systems. nice Run a command with modified scheduling priority

Quick Reference

Command Name:

nice

Category:

process management

Platform:

Linux/Unix

Basic Usage:

nice [options] [arguments]

Common Use Cases

    Syntax

    nice [option] [command [arguments...]]

    Options

    Option Description
    -n, --adjustment=N Add integer N to the niceness (default 10)
    --help Display help and exit
    --version Output version information and exit

    Niceness Values:

    Range Description
    -20 to -1 High priority (requires root privileges)
    0 Default priority
    1 to 19 Low priority (19 is the lowest)

    Examples

    How to Use These Examples

    The examples below show common ways to use the nice command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    nice ls -la
    Run ls command with the default nice value (10).
    nice -n 10 find / -name "*.txt" > results.txt
    Run the find command with a nice value of 10.
    # Advanced Examples Advanced
    nice -n 19 tar -czf archive.tar.gz /large/directory Run tar with the lowest priority (19) to minimize impact on system performance. nice -n -10 ffmpeg -i input.mp4 output.mp4 Run ffmpeg with a high priority (-10) to ensure encoding gets more CPU time (requires root privileges). nice -n 15 make -j4 Compile software with a lower priority (15) to avoid slowing down other processes. nice --adjustment=5 ./backup_script.sh Run a backup script with a niceness level of 5. nice -10 python3 data_processing.py Run a Python script with a nice value of 10. nice -n 10 sleep 60 & nice -n 5 sleep 60 & nice -n 0 sleep 60 Start three sleep processes with different priorities and run them in the background. sudo nice -n -15 rsync -av /source/ /destination/ Run rsync with a high priority (-15) using sudo to gain necessary privileges. nice make && nice -n 0 make test Run make at lower priority but run tests at normal priority.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The 'nice' command in Linux and Unix systems is a simple yet powerful utility that allows users to adjust the scheduling priority of processes. By changing a process's 'niceness' value, users can influence how the CPU allocates time between competing processes, making the system more responsive for critical tasks or reducing the impact of resource-intensive operations. Key features of the nice command: 1. Priority Adjustment: nice allows running commands with a modified scheduling priority. The niceness value ranges from -20 (highest priority) to 19 (lowest priority), with 0 being the default priority for most processes. 2. Privilege Control: Regular users can only increase the niceness value (lower the priority) of their processes, while root users can both increase and decrease niceness values. This security measure prevents ordinary users from monopolizing system resources. 3. Syntax Simplicity: The command has a straightforward syntax, making it easy to use even for those with limited command-line experience. A user simply prepends 'nice' to the command they wish to run with adjusted priority. 4. Default Behavior: When used without the -n option, nice adds a value of 10 to the current niceness, effectively lowering the priority of the specified command. 5. Integration with Other Commands: nice can be combined with virtually any other command or process in the system, allowing for fine-grained control over system resource allocation. Common use cases for nice include: - Running resource-intensive operations (like video encoding, data analysis, or backups) at lower priority to minimize impact on system responsiveness - Allocating more CPU time to important processes by running them with negative niceness values (requires root privileges) - Managing CPU resources on multi-user systems to ensure fair distribution - Prioritizing critical system services over less important background tasks - Running compilation jobs at lower priority to keep the system responsive during development It's important to note that 'nice' only affects CPU scheduling priority, not other resource allocations like memory or I/O operations. For more comprehensive resource control, Linux offers other mechanisms like cgroups or the 'ionice' command for I/O scheduling priorities. The niceness system in Linux is part of the scheduler's time-sharing algorithm. Processes with higher niceness values (lower priorities) receive fewer time slices from the CPU scheduler, while processes with lower niceness values (higher priorities) receive more. This doesn't mean that low-priority processes won't run—they just receive proportionally less CPU time when the system is under load. To check the current niceness value of running processes, users can use commands like 'ps -o nice' or 'top', which display the 'NI' (niceness) column alongside other process information. Complementary to the 'nice' command is 'renice', which allows changing the priority of already-running processes rather than only at launch time.

    Related Commands

    These commands are frequently used alongside nice or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the nice command works in different scenarios.

    $ nice
    View All Commands