Bash: Terminate on Timeout/File Overflow while Executing Command

后端 未结 3 1175
既然无缘
既然无缘 2021-01-28 02:57

I\'m writing a mock-grading script in bash. It\'s supposed to execute a C program which will give some output (which I redirect to a file.) I\'m trying to (1) make it timeout af

3条回答
  •  花落未央
    2021-01-28 03:44

    There's a GNU coreutil command timeout to do timeouts.

    Investigate ulimit -f 32 to set the maximum file size (to 16 KiB; it counts in 512 byte blocks).

    Objection:

    ulimit is [not] suitable because I have to create other files as well. I need to limit only one of them.

    Counter: Unless the program must create a big file and a little file and you have to limit just the little file, you can use a sub-shell to good effect:

    (
    ulimit -f 32
    timeout 10m -- command arg >file
    )
    

    The limit on file size is restricted to the commands in the sub-shell (which is marked by the pair of parentheses).

提交回复
热议问题