How to run a shell script in background without showing in the terminal?

Hi Guys,
i am having a script which checks for ip address is pingable or not,when i execute this script in terminal it keeps on showing the pinging status of every ip address and it takes more time when i check for 100 ip address,How to do run a script in background without showing in the terminal & how to speed up the process?.One thing is i have to give the input in the terminal only.

#here is my pingscript

echo "Enter the initial ip:"
read inip
echo "Enter the end ip:"
read endip
a=$(echo $inip | awk -F. '{print $4}')
b=$(echo $endip | awk -F. '{print $4}')
c=$(echo $inip | head -c10)

pingfunc ()
{
for (( i=$a; i<=$b; i++ ))
do 
if ping -c 4 $c$i
then
echo "pingable"
else
echo "not pingable"
fi
done
}
pingfunc
echo "Done"

Try this

pingfunc ()
{
for (( i=$a; i<=$b; i++ ))
do 
/bin/ping -c 1 $c$i > /dev/null 2>&1 && echo $c$i is pingable || echo no answer from $c$i
done
}

I have changed the count from 4 to 1 on the grounds that unless you have a really bad network one ping is enough.
The redirection gets rid of the verbose output from the ping utility.
I've added the IP address to the output so you can see at a glance which machine is pingable.

Alternatively if you need the four pings per machine try this instead:

/bin/ping -c 4 -i 0.2 $c$i > /dev/null 2>&1 && echo $c$i is pingable || echo no answer from $c$i

This will alter the interval between pings. According to the manual 0.2 seconds is the smallest interval a normal user can specify.

None of the above backgrounds the process but it should certainly be faster than before.

Andrew

1 Like

Thanks Andrew,Its working as expected
can u explain me this line

/bin/ping -c 4 -i 0.2 $c$i > /dev/null 2>&1 && echo $c$i is pingable || echo no answer from $c$i

Why are u using /bin , /dev/null 2>&1
plz explain me

Hello:

1) /bin/ping is the absolute path to the executable

2) The null device is typically used for disposing of unwanted output streams of a process, or as a convenient empty file for input streams. This is usually done by redirection.

3) In a Unix shell, if I want to combine stderr and stdout into the stdout stream for further manipulation, I can append the following on the end of my command: 2>&1

Regards

In addition to what it have been explained, already, the first > is the short form of 1> or redirect standard output. (1)
2>&1 redirects standard error (2) (reported messages related to the execution of the program) to the same place that standard output (1) points to.
If you where t do 2>1 , the stderr will be redirected to a file (created if it doesn't exist) named 1 , instead of stdout . The way to let the system know that you really mean redirection to standard output is to add the & in front.