Reputation: 1765
I have a script e. g. a.sh, which calls script b.sh.
After execution, I want that the b.sh will be interrupt after 5 secs and then it will be executed again.
Any ideas, how I can run it? Thanks
Upvotes: 0
Views: 897
Reputation: 644
CTRL+C is sending the SIGINT
command. By default the timeout
is sending the SIGTERM
signal, thus making the accepted solution inaccurate. If you really want to send the SIGINT
you can do it by passing:
timeout -s SIGINT 5 b.sh
You can also pass different signals to the timeout
command. You can see a list of all of them by executing kill -l
.
And you can do the loop with while
as suggested in the other comments.
Upvotes: 1
Reputation: 60077
a
can kill b
with -INT
. That pretty much has the effect of Ctrl-C
:
a_sh()
{
b_sh &
pid=$!
while :; do
sleep 5
kill -INT $pid
done
}
b_sh()
{
trap 'echo ouch' SIGINT #if this isn't here, b will die after 5 seconds instead of just saying 'ouch'
i=0;
while :; do
echo loop $i
sleep 1;
i=$((i+1))
done
}
a_sh
Upvotes: 0
Reputation: 14510
You could put the calling in a loop and use timeout
to kill the second script after a certain amount of time, like say
while true; do
timeout 5s b.sh
done
unless you don't really want it looping endlessly, if you just want it to timeout then execute once more:
timeout 5s b.sh
b.sh
Upvotes: 0