Home   Profile   Fun
#127 Linux  03.06.2007

How to change the scheduling priority of CGIs


Some CGI scripts or programms use a lot of CPU power. An example is htsearch which is the search engine of the htdig package. It is used to index and search webpages. Depending on the number of visitors and the size of the database htsearch can easily bring your system to a standstill. One possible way to deal with this situation is to nice htsearch. With nice the process can be started with a lower scheduling priority and allows the system to remain stable with more htsearch users. Of course the downside is that the search needs more time to complete. To run htsearch with nice you can write a simple perl wrapper htsearch.cgi and insert the value for the new scheduling priority
#!/usr/bin/perl
use CGI::Carp qw(fatalsToBrowser);
print `nice -n 10 ./htsearch`;
exit 0;

Then create a symbolic link from the wrapper to the actual binary
ln -s ./htsearch ./htsearch.cgi

When htsearch.cgi is called from an HTML form the process is run with 10. Normally it is run with 0 which is the default value. The higher the value the longer the process needs to complete.

An already running process can be changed with renice:
renice 10 -p 8187
10 is the new priority and 8187 is the PID.

The value for the priority can range from -20 (fastest) to 19 (slowest). The nicer the slower.