如何在Bash中运行带有超时的进程? [英] How to run a process with a timeout in Bash?
问题描述
可能重复:
在给定时间后杀死子进程的Bash脚本超时
Possible Duplicate:
Bash script that kills a child process after a given timeout
有没有一种方法可以编写将执行某条命令15秒钟然后杀死该命令的Shell脚本?
Is there a way to write a shell script that would execute a certain command for 15 seconds, then kill the command?
我尝试了睡眠,等待和ping操作,但也许我在错误地使用它们.
I have tried sleep, wait and ping but maybe I am using them wrong.
推荐答案
使用timeout
命令:
timeout 15s command
注意:在某些系统上,您需要安装coreutils
,在其他系统上,则需要安装coreutils
或具有不同的命令行参数.请参阅@ArjunShankar发布的替代解决方案.在此基础上,您可以封装样板代码,并创建自己的可移植timeout
脚本或执行相同操作的小型C应用程序.
Note: on some systems you need to install coreutils
, on others it's missing or has different command line arguments. See an alternate solution posted by @ArjunShankar . Based on it you can encapsulate that boiler-plate code and create your own portable timeout
script or small C app that does the same thing.
这篇关于如何在Bash中运行带有超时的进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!