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.
假设我运行apt-get update,我想在N段时间内停止它,然后继续我的脚本。
1 个解决方案
#1
0
This is an example of a sh script that make use of the "timeout" command to:
这是一个使用“timeout”命令的sh脚本示例:
Run "sleep 30", timeout on 10 seconds, then kill "TERM", wait 3s, then kill -9
运行“睡眠30”,超时10秒,然后杀死“TERM”,等待3秒,然后杀死-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
#1
0
This is an example of a sh script that make use of the "timeout" command to:
这是一个使用“timeout”命令的sh脚本示例:
Run "sleep 30", timeout on 10 seconds, then kill "TERM", wait 3s, then kill -9
运行“睡眠30”,超时10秒,然后杀死“TERM”,等待3秒,然后杀死-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