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

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to run a shell script in background without showing in the terminal?
# 1  
Old 11-25-2016
Linux 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
Code:
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"

# 2  
Old 11-25-2016
Try this
Code:
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:
Code:
/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
This User Gave Thanks to apmcd47 For This Post:
# 3  
Old 11-25-2016
Thanks Andrew,Its working as expected
can u explain me this line
Code:
/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
# 4  
Old 11-25-2016
Explain !

Quote:
Originally Posted by Meeran Rizvi
Thanks Andrew,Its working as expected
can u explain me this line
Code:
/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:
Code:
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
# 5  
Old 11-25-2016
Quote:
Originally Posted by Meeran Rizvi
Thanks Andrew,Its working as expected
can u explain me this line
Code:
/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
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.

Last edited by Aia; 11-25-2016 at 11:59 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to run multiple functions in Background in UNIX Shell Scripting?

Hi, I am using ksh , i have requirement to run 4 functions in background , 4 functions call are available in a case that case is also in function, i need to execute 1st function it should run in background and return to case and next i will call 2nd function it should run in background and... (8 Replies)
Discussion started by: karthikram
8 Replies

2. Shell Programming and Scripting

Script working when run manually but not in crontab showing path not found

i have a script running using variable defined in .profile when i run that script manually its working but when i run the same script through cron its giving path not found I had defined path in .profile (3 Replies)
Discussion started by: raj_saini20
3 Replies

3. Shell Programming and Scripting

Run command in background thru script

Dear All, Writing a script in which I want to run a command in background and keep it running even script is finished. I have tried like below, `truss -p <pid> >> & /tmp/log &` But doesnt work.. script goes running and nothing in log file. (7 Replies)
Discussion started by: Deei
7 Replies

4. Shell Programming and Scripting

user attributes not showing when I run the script

I am new to linux/unix scripting and working in one company on linux project. I got a script that when it executes should give us the users atributes showing who is retriving data? the script should show us who are the users reriving information. I ran that script as sudo ./test4 but finding the... (0 Replies)
Discussion started by: starter2011
0 Replies

5. Shell Programming and Scripting

Need to run the script in background, but having a problem

hi, we have a script which runs for the whole day and whenever the job fails, will send an alert to the mailbox. My problem here is that i need to give the jobname dynamically which is not possible if we run the script in background. Pls help me with this. Thanks Ajay (6 Replies)
Discussion started by: ajayakunuri
6 Replies

6. Shell Programming and Scripting

shell script does not work if run in background

Dear All, I am trying to run a script in background like ./scriptname.sh & but when i try to run it in background it is giving me an error "syntax error at line 12: `(' unexpected" at the line 12, there is a function definition "function getFileList()". This script runs fine if i run on... (2 Replies)
Discussion started by: bilalghazi
2 Replies

7. Shell Programming and Scripting

open terminal to run cmd using shell script

i want the shell script to open the terminal and in that terminal i want to run a command specified in the script... how can it be done... (2 Replies)
Discussion started by: chandrabhushan
2 Replies

8. Shell Programming and Scripting

not showing restore information while run script

Hello, I have the following script to restore file and grep information. However, once it restore file, it showing a lot useless information and different to check which file have the statement "John price $200". Can I not show any information while running script. It only show..when found the... (1 Reply)
Discussion started by: happyv
1 Replies

9. Shell Programming and Scripting

how to run script at background

Hi all, I have a script like: echo Please input list file name: read listn for file in `cat $listn.txt` do send_file $file done normally, I will run the script like: :. resendfile Please input list filename: list1 #Then, the script will resend all file from the list1. However,... (4 Replies)
Discussion started by: happyv
4 Replies

10. Shell Programming and Scripting

run a shell in the background

How can I run a shell in the background? cat test.sh #!/bin/sh sleep 600 Thank u very much indeed! (2 Replies)
Discussion started by: GCTEII
2 Replies
Login or Register to Ask a Question