我正在编写一个脚本,在某些时候我调用了“command1”,它在调用 CTRL+C 之前不会停止。
- 有没有办法为命令声明超时?喜欢:command1 参数 -timeout 10
- 如何以文本形式编写 CTRL+C 命令?
谢谢!
您可以使用timeout
来自 GNU coreutils 的命令(您可能需要先安装它,但它包含在大多数(如果不是全部)Linux 发行版中):
timeout [OPTION] DURATION COMMAND [ARG]...
例如:
timeout 5 ./test.sh
将在执行 5 秒后终止脚本。如果要发送 KILL 信号(而不是 TERM),请使用-k
flag。
这里有timeout
命令的完整描述。
我刚试过
jekyll -server & sleep 10;pkill jekyll
可以为你的情况做。
在您的脚本中,您可以设置等待。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