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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.