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.

4
  • Can you show your script in question? Commented Feb 23, 2015 at 12:37
  • Did you look at the timeout command? Commented Feb 23, 2015 at 12:41
  • echo -e "Enter monitor card id: \c " read monitorcard echo "Monitor id is: $monitorcard " echo "Starting Scan For Wps " eval 'wash -i $monitorcard ' No i haven't yey.. but i will Commented Feb 23, 2015 at 12:44
  • 1
    have a look here stackoverflow.com/questions/687948/… Commented Feb 23, 2015 at 12:51

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.