Kill and Sleep command Not working properly in bas

2019-09-11 16:28发布

问题:

I have a bash script:

#!/bin/sh
for ((i=0; i<=10; i++)); do 
    ssh "w$i" 'uptime;ps -elf|grep httpd|wc -l;free -m;mpstat'
done &
pid=$!
sleep 3
kill -9 $pid

I want to kill the ssh within the for loop, if it does not complete within 3 seconds. But my observations are, that the process is not killed and that the loop takes more than 3 seconds to kill.

Can you please help me to correct my script so my script can kill the for loop process after 3 seconds?