Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux commands - abort operation with timeout

I'm writing a script and at some point I call to "command1" which does not stop until it CTRL+C is invoked.

  1. Is there a way to state a timeout for a command? Like: command1 arguments -timeout 10
  2. How do I write a CTRL+C command in textual form?

Thx!

like image 231
user1413824 Avatar asked Jun 15 '12 13:06

user1413824


People also ask

How do I set timeout in Linux?

The syntax for the timeout command is as follows: timeout [OPTIONS] DURATION COMMAND [ARG]… The DURATION can be a positive integer or a floating-point number, followed by an optional unit suffix: s - seconds (default)

How do I stop timeout in Linux?

The “timeout” command uses the “SIGTERM” to stop a process, but many processes ignore the “SIGTERM” signal. To forcefully terminate a process using the “SIGKILL” signal, cannot be ignored by any process. The “-k” option is being used to terminate the process.

How do you abort a command in Linux?

Hold the Ctrl button and press the C key at the same time. It sends the SIGKILL signal to the running program to force quit the command.


1 Answers

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

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

For instance:

timeout 5 ./test.sh

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

Here you have the full description of the timeout command.

like image 91
betabandido Avatar answered Sep 30 '22 14:09

betabandido