Limiting the time a program runs in Linux

后端 未结 7 1857
礼貌的吻别
礼貌的吻别 2020-12-23 11:39

In Linux I would like to run a program but only for a limited time, like 1 second. If the program exceeds this running time I would like to kill the process and show an erro

7条回答
  •  情书的邮戳
    2020-12-23 11:53

    If you have the sources, you can fork() early in main() and then have the parent process measure the time and possibly kill the child process. Just use standard system calls fork(), waitpid(), kill(), ... maybe some standard Unix signal handling. Not too complicated but takes some effort.

    You can also script something on the shell although I doubt it will be as accurate with respect to the time of 1 second.

    If you just want to measure the time, type time on the shell.

提交回复
热议问题