我正在写一个脚本,在某些时候,我调用“command1”,它不会停止,直到它被调用CTRL + C。
谢谢!
你可以使用来自GNU coreutils的timeout
命令(你可能需要先安装它,但是如果不是所有的Linux发行版,都会有这个命令):
timeout [OPTION] DURATION COMMAND [ARG]...
例如:
timeout 5 ./test.sh
将在执行5秒后终止脚本。 如果你想发送一个KILL信号(而不是TERM),使用-k
标志。
在这里你可以完整的描述timeout
命令。
我刚试过
jekyll -server & sleep 10;pkill jekyll
可以为你的情况做。
在你的脚本中你可以设置一个等待。 wait 10
会等待10秒钟,而退出程序时不用CTRL + C
查看exit
命令。 如果你使用exit 0
那就意味着好的。 有不同的版本,但是我不知道它们是什么意思。
exit 1 exit 2..... so on and so forth
更新
@ Celada
不需要猛击。 你可能只是说“也许你没有正确理解这个问题”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