Bash: Terminate on Timeout/File Overflow while Executing Command

后端 未结 3 1171
既然无缘
既然无缘 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:47

    you can use timeout command eg

    timeout -s 9 5s ./c_program > file
    

    to check file size, you can stat the file, then do if/else

    limit=1234 #bytes
    size=$(stat -c "%s" file)
    if [ "$size"  -gt "$limit" ] ;then
      exit
    fi
    

    see also here if you can't use these GNU tools, or here for some other inspirations.

提交回复
热议问题