How to set cpu limit for given process permanently. Cpulimit and nice don't work

There are many similar questions but none of them help me with my problem.

I have ubuntu 11.10 on server (it's desktop version). I have a running website on it. There is one process that is very important, but don't have to be run on high priority. I want to permanently limit cpu usage for process, not for user. This process is run by exec function (it's php function, not system process).

So I see 2 options: 1. Add some kind of limit every time function is executed. 2. Limit cpu usage permanently for this process.

I have tried to use "nice" and cpulimit, but nothing seems to work. Nice don't have any effect and cpulimit (with -e) says: "no target process found".

I am quite a beginer, so please assume I know almost nothing.


In the absence of requested details ...

Here is how I use cgroups on ubuntu.

Throughout this post, you will need to change the variable "$USER" to the user running the process

I added information for memory as well as that is going to be a FAQ, if you do not need it do not use it.

1) Install cgroup-bin

sudo apt-get install cgroup-bin

2) Reboot. cgroups is now located at /sys/fs/cgroup

3) Make a cgroup for your user (the owner of the process)

# Change $USER to the system user running your process.
sudo cgcreate -a $USER -g memory,cpu:$USER

4) Your user can them manage resources. By default users get 1024 cpu units (shares), so to limit to about 10 % cpu , memory is in bytes ...

# About 10 % cpu
echo 100 > /cgroup/cpu/$USER/cpu.shares

# 10 Mb
echo 10000000 > /cgroup/memory/$USER/memory.limit_in_bytes

5) Start your process (change exec to cgexec)

# -g specifies the control group to run the process in
# Limit cpu
cgexec -g cpu:$USER command <options> &

# Limit cpu and memory
cgexec -g memory,cpu:$USER command <options> &

Configuration

Assuming cgroups are working for you ;)

Edit /etc/cgconfig.conf , add in your custom cgroup

# Graphical
gksudo gedit /etc/cgconfig.conf

# Command line
sudo -e /etc/cgconfig.conf

Add in your cgroup. Again change $USER to the user name owning the process.

group $USER {
# Specify which users can admin (set limits) the group
perm {    
    admin {
        uid = $USER;
    }
# Specify which users can add tasks to this group
    task {
        uid = $USER;
    }
}
# Set the cpu and memory limits for this group
cpu {
    cpu.shares = 100;
    }
memory {
    memory.limit_in_bytes = 10000000;
    }
}

You can also specify groups gid=$GROUP , /etc/cgconfig.conf is well commented.

Now again run your process with cgexec -g cpu:$USER command <options>

You can see your process (by PID) in /sys/fs/cgroup/cpu/$USER/tasks

Example

bodhi@ufbt:~$ cgexec -g cpu:bodhi sleep 100 &

[1] 1499

bodhi@ufbt:~$ cat /sys/fs/cgroup/cpu/bodhi/tasks

1499

For additional information see: http://docs.redhat.com/docs/en-US/Red_Hat_Enterprise_Linux/6/html/Resource_Management_Guide/