How to Launch a Process with Custom Priority (Lower nice Value) in RHEL


2 views

In Linux systems (including RHEL), process priority is managed through nice values ranging from -20 (highest priority) to 19 (lowest priority). Contrary to what the name suggests, a lower nice value means higher CPU scheduling priority.

While you can use nice to launch processes with increased priority (lower nice values) or renice to adjust running processes, these solutions have limitations:

  • nice only increases priority (lowers nice value)
  • renice requires a PID and works only on running processes
  • Neither provides a clean solution for daemon startup

The most straightforward solution is to use nice with a negative adjustment:

nice -n -2 /path/to/your/daemon --arguments

This launches your daemon with a nice value of -2 (higher priority than default 0).

For modern RHEL systems using systemd, you can configure priority directly in the service file:

[Service]
ExecStart=/path/to/your/daemon --arguments
Nice=-2
CPUSchedulingPolicy=rr
CPUSchedulingPriority=50

Combine priority control with CPU pinning for critical processes:

taskset -c 0 nice -n -2 /path/to/daemon

After launch, verify with:

ps -eo pid,ni,cmd | grep your_daemon
  • Root privileges are typically required for negative nice values
  • Over-prioritizing can starve other system processes
  • Consider using cgroups for more sophisticated resource control

On Linux systems, process priority works inversely - lower nice values mean higher priority. The default nice value is 0, with the range from -20 (highest priority) to 19 (lowest priority). This often causes confusion when we want a process to get more CPU time during resource contention.

For RHEL systems, you have several clean ways to launch daemons with adjusted priority without resorting to renice:


# Method 1: Using nice at launch
nice -n -18 /path/to/daemon --arguments

# Method 2: Using setsid for daemon processes
setsid nice -n -18 /path/to/daemon --arguments >/dev/null 2>&1 &

# Method 3: Combined with nohup for persistent processes
nohup nice -n -18 /path/to/daemon --arguments > /var/log/daemon.log 2>&1 &

For modern RHEL systems using systemd, create or modify a service unit:


[Service]
ExecStart=/usr/bin/nice -n -18 /path/to/daemon --arguments
Nice=-18
OOMScoreAdjust=-500

After launch, confirm the priority with:


ps -eo pid,ni,cmd | grep daemon_name
# OR for detailed view
top -p $(pgrep -d',' daemon_name)

When adjusting priorities:

  • Avoid setting nice values below -15 for non-critical processes
  • Combine with ionice for disk I/O prioritization
  • For database services, consider chrt for SCHED_RR real-time scheduling

# Example combining CPU and I/O priority
nice -n -15 ionice -c2 -n0 /path/to/database_service