Linux Commands - Timeout Abort

I am writing a script, and at some point I call "command1", which does not stop until CTRL + C is called.

  • Is there a way to specify a timeout for a team? Like: command1 arguments -timeout 10
  • How to write CTRL + C command in text form?

thank!

+5
source share
3 answers

You can use the command timeoutfrom GNU coreutils (you may need to install it first, but it is included in most, if not all, Linux distributions):

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

For instance:

timeout 5 ./test.sh

will complete the script after 5 seconds of execution. If you want to send a KILL signal (instead of TERM), use the flag -k.

Here you have a full description of the team timeout.

+6

jekyll -server & sleep 10;pkill jekyll

.

0

script . wait 10 10 , CTRL + C exit. exit 0, , . , , .

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

Update


@Celada

bash. ", ". 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 
-1

All Articles