shell脚本设置command的timeout

myccc456 2010-07-06 07:36:53
一个shell脚本中, 有些command需要设置timeout,请问怎么办呢?
...全文
791 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljc007 2010-07-30
  • 打赏
  • 举报
回复
./test.sh & { sleep ${TIMEOUT}; eval 'kill -9 $!' &> /dev/null; }
myccc456 2010-07-30
  • 打赏
  • 举报
回复
十分感谢1楼,但是如果服务器很多的话,每个都需要装这个工具的话,也不大现实。
不知道为何unix默认不提供这个命令,perl是可以的。
louyong0571 2010-07-14
  • 打赏
  • 举报
回复
这个,没想法。。
steptodream 2010-07-10
  • 打赏
  • 举报
回复
1楼说的那个工具还没用过
什么时候试试 嘿嘿
shnsunday 2010-07-09
  • 打赏
  • 举报
回复
3小时0分钟以前Ambar Ray
masmaster 2010-07-06
  • 打赏
  • 举报
回复
如果用bash, 可以用他的read -t sec,也可以用stty来实现。
shell的内部变量TMOUT只能限定在命令行下即交互模式
Coder_Granger 2010-07-06
  • 打赏
  • 举报
回复
安装timeout这个工具,
Debian/Ubuntu可以
sudo apt-get install timeout



timeout用法如下
TIMEOUT(1)                                                         TIMEOUT(1)

NAME
timeout - run command with bounded time

SYNOPSIS
timeout [-signal] time command ...

DESCRIPTION
timeout executes a command and imposes an elapsed time limit. The
command is run in a separate POSIX process group so that the right
thing happens with commands that spawn child processes.

Arguments:

-signal
Specify an optional signal to send to the controlled process.
By default, timeout sends SIGKILL, which cannot be caught or
ignored. The signal must be provided in its numerical value.

time The elapsed time limit in seconds after which the command is
terminated.

command
The command to be executed.

DIAGNOSTICS
timeout's exit status is the exit status of the specified command or 1
in case of a usage error.

LICENSE
The IBM PUBLIC LICENSE must be distributed with this
software.

HISTORY
This program was first released as part of SATAN.

AUTHOR(S)
Wietse Venema

TIMEOUT(1)

19,619

社区成员

发帖
与我相关
我的任务
社区描述
系统使用、管理、维护问题。可以是Ubuntu, Fedora, Unix等等
社区管理员
  • 系统维护与使用区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧