Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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 stackoverflow.com 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 <input.in> ouput.out and the second one ulimit -t 1; ./b <input.in> 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
add comment

3 Answers

Did you follow the instructions in the answer to your previous question exactly? The parentheses are not optional - they create a subshell.

share|improve this answer
    
yes, of course. the first command in work well, like I said. But the second one, when I tried to increased the cpu time limit, it cause an error.. any suggestion for me? thx.. –  Bobby Stenly May 16 '11 at 15:06
add comment
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
add comment

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 <input.in> 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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.