Search code examples
phplinuxbashruntimememory-limit

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


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..


Solution

  • 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