1

I have a script that after a while executes a command that cannot stop unless you terminate it. How do I stop the command and continue my script?

Let's say I run apt-get update and I want to stop it in a N period of time and then continue my script.

l0b0
  • 55,365
  • 30
  • 138
  • 223
Ariox
  • 11
  • 2

1 Answers1

1

This is an example of a sh script that make use of the "timeout" command to:

Run "sleep 30", timeout on 10 seconds, then kill "TERM", wait 3s, then kill -9

TIMEOUT_TO_SIGNAL=10
SIGNAL_AFTER_TIMEOUT=TERM
WAIT_FOR_KILL="3s"
COMMAND_TO_EXEC="sleep 30"

echo "Run \"$COMMAND_TO_EXEC\", timeout on $TIMEOUT_TO_SIGNAL seconds, then kill \"$SIGNAL_AFTER_TIMEOUT\", wait $WAIT_FOR_KILL, then kill -9"
timeout --signal=$SIGNAL_AFTER_TIMEOUT --kill-after=$WAIT_FOR_KILL $TIMEOUT_TO_SIGNAL $COMMAND_TO_EXEC

if [ $? -eq 124 ]
then
    echo "The command $COMMAND_TO_EXEC timed out"
else
    echo "The command $COMMAND_TO_EXEC executed without timeout"
fi
dAm2K
  • 9,923
  • 5
  • 44
  • 47