Current Location: Home> Latest Articles> How to set the CPU time slice priority of a process using proc_nice in PHP?

How to set the CPU time slice priority of a process using proc_nice in PHP?

gitbox 2025-05-29

What is proc_nice() ?

proc_nice() is a system call interface provided by PHP, which is used to change the "nice value" of the current process. The so-called "nice value" is an indicator for measuring process priorities in Unix-like systems. The smaller the value, the higher the priority; the larger the value, the lower the priority. The range is generally from -20 to 19, and the default is 0.

 <?php
$originalNice = proc_nice(0); // Query the current nice value
echo "Current process nice value为: $originalNice\n";

// Improve priority(Reduce nice value)
if (proc_nice(-10)) {
    echo "Successfully upgraded process priorities。\n";
} else {
    echo "Failed to improve process priority(May be required root Permissions)。\n";
}
?>

Things to note

  1. Permission limit : Reducing nice value (raising priority) usually requires higher permissions, such as root. Normal users can only increase the nice value by default (i.e., reduce priority).

  2. Temporary effect : Using proc_nice() will only affect the current script process and will not permanently change the system settings.

  3. Cross-platform difference : Windows systems do not support proc_nice() , and calls on this platform will fail.

Typical usage scenarios

Suppose you are writing an image compression batch processing tool, and the user uploads hundreds of images at a time, the processing process may be very time-consuming. You can use proc_nice() to increase the execution priority of the script, allowing it to use as much CPU as possible, and shorten the total processing time.

 <?php
proc_nice(-5); // Try to increase process priorities slightly

$images = glob('/path/to/uploads/*.jpg');

foreach ($images as $image) {
    $compressed = compressImage($image);
    file_put_contents('/path/to/compressed/' . basename($image), $compressed);
}

function compressImage($file) {
    // Sample compression logic(pseudocode)
    $img = imagecreatefromjpeg($file);
    ob_start();
    imagejpeg($img, null, 60);
    $data = ob_get_clean();
    imagedestroy($img);
    return $data;
}
?>

When compatible with a task queue (such as Redis-based message queue), you can also prioritize tasks to the CPU by setting higher priority for processing scripts.

More effective in CLI mode

Since most web servers have execution time limits on PHP (such as 30 seconds) and have relatively limited operating environments, it is recommended to use proc_nice() in command line (CLI) mode, for example:

 php /var/www/gitbox.net/scripts/image_compress.php

This not only allows you to freely adjust the nice value, but also allows you to schedule tasks through crontab, systemd, etc.


Advanced: Used in combination with nice command

If you cannot call proc_nice() inside PHP, or run it in a restricted host environment, you can use the operating system's nice command to start your PHP script externally:

 nice -n -10 php /var/www/gitbox.net/scripts/heavy_task.php

This also enables priority control, suitable for deployment in production environments.