I am trying to write a bash script in a file that would, when run start pinging a host until it becomes available, when the host becomes reachable it runs a command and stops executing, I tried writing one but the script continues pinging until the count ends,
Plus I need to put that process in the background but if I run the script with the dollar ($
) sign it still runs in foreground,
#!/bin/bash
ping -c30 -i3 192.168.137.163
if [ $? -eq 0 ]
then /root/scripts/test1.sh
exit 0
else echo “fail”
fi
Best Answer
I would use this, a simple one-liner:
Replace
HOSTNAME
with the host you are trying to ping.I missed the part about putting it in the background, put that line in a shellscript like so:
And to background it you would run it like so:
Again replace
HOSTNAME
with the host you are trying to ping. In this approach you are passing the hostname as an argument to the shellscript.Just as a general warning, if your host stays down, you will have this script continuously pinging in the background until you either kill it or the host is found. So I would keep that in mind when you run this. Because you could end up eating system resources if you forget about this.