Linux bash inline命令执行程序并限制资源

Linux bash inline命令执行程序并限制资源

问题描述:

I made a website that needs to execute some kind of C++ program (the program has been compiled). But I want to limit the resources needed by the program. The resources that I want to limit are run time and used memory. I think run time can be done with command to execute & sleep time; kill $! But that command causes some delay if the process finishes before the sleep time.

I don't know how to limit run time (without delay) and used memory with an inline command. Is there any suggestion to do it?

Thx before... I really appreciate your help..

command ulimit is your friend

for example (ulimit -t 1; /your/program 2>/some/logfile) | head -c 100k

will do two things:

  • limit the max CPU to one second
  • limit the output to 100k

Check out the ulimit command. I've not used it but I think it will do what you want.

(ulimit -d 1000 -m 1000 -v 1000 -t 1; /your/program 2>/some/logfile) | head -c 100000

This will limit memory size as well as time. You can use ulimit -a to find out what these limits mean or read the bash man page and search for ulimit.