Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to limit time and memory resources for running a bash command?

Tags:

bash

Basically I want to run my compiled C++ code and limit execution time (to a second for example) and memory (to 100k) like the online judges. Is it possible by adding options to the command? This has to be done without modifying the source code of course.

like image 278
kanslulz Avatar asked Feb 09 '23 00:02

kanslulz


1 Answers

Try ulimit command, it can set limits on CPU time and memory.

Try this example

bash -c 'ulimit -St 1 ; while true; do true; done;'

The result you will get will be

CPU time limit exceeded (core dumped)
like image 104
Grzegorz Żur Avatar answered Jun 01 '23 09:06

Grzegorz Żur