Question

I have two scripts:

### firstScript.sh ###
trap 'kill $pid;' SIGINT
for ((i=1; i<=10; i++))
do
        echo "$i x $1 = `expr $i \* $1`"
        sleep 10 &
        pid=$!
        wait $!
done

And:

### secondScript.sh ###
./firstScript.sh 5 &
pid=$!
echo $pid
ps
for (( i=0; i<8; i++))
do
        kill -2 $pid
        sleep 3
done

The 2nd script will call the first script in background mode and send it a SIGINT signal every 3 seconds. However, it doesn't work at all.

I tried run the 1st script in background with ./firstScript.sh 5 & and send a signal with kill -2 %1 or kill -2 <pid> (with the pid was obsevred after creating the 1st script) and both of them worked fine.

However, I couldn't do it by the script.

Appreciate for any help.

Was it helpful?

Solution

If you are looking to make this work as it appears you intend it to, use a different signal, such as SIGUSR1:

### firstScript.sh ###
trap 'kill $pid;' SIGUSR1
for ((i=1; i<=10; i++))
do
        echo "$i x $1 = `expr $i \* $1`"
        sleep 10 &
        pid=$!
        wait $!
done

And:

### secondScript.sh ###
./firstScript.sh 5 &
pid=$!
echo $pid
ps
for (( i=0; i<8; i++))
do
        kill -USR1 $pid
        sleep 3
done

I have spent some time trying to figure out why SIGINT is behaving the way it does, but have not been able to yet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top