Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've posted a question about this few hours ago on Stack Overflow:

Linux bash inline command to execute a program and limit the resource

As I know, to limit the resource I can use ulimit command.

But, the problem is when I set the CPU time limit 1 second, and then I want to execute another program with CPU limit 2s, the ulimit command return an error like this:

bash: ulimit: CPU time: cannot modify limit: Operation not permitted

and absolutely my program killed in 1 second.

So, How can I make the second program running with the CPU time limit 2 seconds?

share|improve this question

migrated from May 16 '11 at 15:20

This question came from our site for professional and enthusiast programmers.

are you executing as super user? – phoxis May 16 '11 at 14:22
How exactly are you running the commands? – jcollie May 16 '11 at 14:26
@phoxis : nope.. @jcollie : ulimit -t 1; ./a <> ouput.out and the second one ulimit -t 1; ./b <> output.out – Bobby Stenly May 16 '11 at 14:40
i think you need to run it as super user. – phoxis May 16 '11 at 14:41
is there any way to do it not in super user? because I want to do it from php script. thx for your help.. – Bobby Stenly May 16 '11 at 14:49
up vote 0 down vote accepted

Finally I found my answer after a few times carefully reading about ulimit... I found my mistake... I had a wrong perception about soft limit and hard limit, and what I need to do is use soft limit for the command.. and I done it with ulimit -St 1; ./a ouput.out and for the second one ulimit -St 2; ./a ouput.out

I'm sorry for my bad... wish this post would be helpful for everyone who need it... Thanks very much..

share|improve this answer

It's usually better to give priorities to processes rather than limiting it's resources. Why don't let the process run at full power if the machine is idle?

You can use nice to run commands with lower or higher priority. You only need superuser access if you wish to run with higher priority.

$ nice [-n increment] ./a <> output.out

You can also use renice to change the priority of process that are already running.

Both nice and renice can receive an optional parameter -n with an integer (-20 <= n <= 20). Positive integer make the process nicer and use less resources, while negative integers will make it less nice. If no parameter is given 10 is assumed.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .