Bash: Terminate on Timeout/File Overflow while Executing Command

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

    This starts yourcommand, redirecting output via dd to youroutputfile and putting a limit of 10000000 bytes on it: dd will terminate and SIGPIPE will be sent to yourcommand

    yourcommand | dd of=youroutputfile bs=1 count=10000000 &
    

    This will wait 5 seconds and kill yourcommand if not already terminated:

    sleep 5
    kill %yourcommand
    

提交回复
热议问题