5

我正在编写一个脚本,在某些时候我调用了“command1”,它在调用 CTRL+C 之前不会停止。

  1. 有没有办法为命令声明超时?喜欢:command1 参数 -timeout 10
  2. 如何以文本形式编写 CTRL+C 命令?

谢谢!

4

3 回答 3

7

您可以使用timeout来自 GNU coreutils 的命令(您可能需要先安装它,但它包含在大多数(如果不是全部)Linux 发行版中):

timeout [OPTION] DURATION COMMAND [ARG]...

例如:

timeout 5 ./test.sh

将在执行 5 秒后终止脚本。如果要发送 KILL 信号(而不是 TERM),请使用-kflag。

这里timeout命令的完整描述。

于 2012-06-15T13:24:36.247 回答
0

我刚试过

jekyll -server & sleep 10;pkill jekyll

可以为你的情况做。

于 2012-06-15T13:24:20.960 回答
-1

在您的脚本中,您可以设置等待。wait 10将等待 10 秒,以及在不CTRL + C查看exit命令的情况下退出程序。如果你使用exit 0它意味着好的。有不同的版本,但我不知道它们到底是什么意思。

exit 1
exit 2..... so on and so forth

更新


@塞拉达

没必要打。您可能只是说“也许您没有正确理解这个问题” Stackoverflow 在这里帮助人们学习,而不是拆毁他们。他们为此发明了 Reddit。至于退出代码,您可以通过发出带有代码的 exit() 命令来强制程序退出。直接来自linux.die.net

Exit Code Number Meaning Example Comments 
1 Catchall for general errors let "var1 = 1/0" Miscellaneous errors, such as "divide by zero" 
2 Misuse of shell builtins (according to Bash documentation)   Seldom seen, usually defaults to exit code 1 
126 Command invoked cannot execute   Permission problem or command is not an executable 
127 "command not found"   Possible problem with $PATH or a typo 
128 Invalid argument to exit exit 3.14159 exit takes only integer args in the range 0 - 255 (see footnote) 
128+n Fatal error signal "n" kill -9 $PPID of script $? returns 137 (128 + 9) 
130 Script terminated by Control-C   Control-C is fatal error signal 2, (130 = 128 + 2, see above) 
255* Exit status out of range exit -1 exit takes only integer args in the range 0 - 255 
于 2012-06-15T13:25:34.233 回答