Mark Delphi
Mark Delphi

Reputation: 1765

How to send CTRL+C without press?

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

Answers (4)

Georgi Stoyanov
Georgi Stoyanov

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

Petr Skocik
Petr Skocik

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

Eric Renouf
Eric Renouf

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

Cyrus
Cyrus

Reputation: 88674

while true; do timeout 5 /path/to/b.sh; done

Upvotes: 1

Related Questions