LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   time script without using crontab (https://www.linuxquestions.org/questions/linux-newbie-8/time-script-without-using-crontab-809621/)

sunlinux 05-23-2010 10:00 AM

time script without using crontab
 
hey guys I want to write a ping script that ping a ip after every 60 sec and log output to a file.

but I don want to use crontab for that. pls suggest

Simon Bridge 05-23-2010 10:19 AM

sounds like homework - read the man page for the ping command, it is all there.

man ping

yooy 05-23-2010 10:35 AM

while true
do
sleep 60
ping www.google.com
done

add output loging and run in background

sunlinux 05-23-2010 01:48 PM

hi below script giving error

./keep.sh: line 7: [: missing `]'


[root@localhost sun]# vi keep.sh
HOSTS=192.5.14.1
COUNT=4
while true
do
sleep 60
count=$(ping -c $COUNT $myHost | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [$count -eq 0]; then
echo "Host : $myHost is down (ping failed) at $(date)" >> /var/sun/status
fi
done

what i want is pinging this ip in every 60 secs n recording output to status file

Simon Bridge 05-23-2010 02:24 PM

That is not the answer you want - read through the manual page carefully and consider what the words "interval" and "count" mean in that document. Use a pipe to send the output to a file. It can be done in a single line.

@yooy: you can help people with their homework, but don't do it for them. The idea is they are supposed to learn by working it out themselves as much as possible. You should also check out the solution suggested in the manual and try piping the output to see what happens. See if you cannot do it in one line too.

MTK358 05-23-2010 03:10 PM

Quote:

Originally Posted by sunlinux (Post 3978641)
[$count -eq 0]

Put a space between the "0" and "]". BASH understands them as strings, not numbers.

The [ command is an alias for the test command, that expects a "]" as the last argument.

chrism01 05-23-2010 06:23 PM

Just FYI, double brackets ie [[ ]] is preferred http://www.tldp.org/LDP/abs/html/tes...ml#DBLBRACKETS

Simon Bridge 05-25-2010 06:19 AM

Now I'm feeling suspicious. Surely this is a trivial homework problem and you guys are just pulling his leg?

He wants to execute ping 192.5.14.1, a count of four times, at 1min intervals, and pipe the output to a text file. Surely that is just a one-liner off the man page - what's with the while loop? Doesn't ping have an option for that?

sunlinux 05-26-2010 12:45 PM

Guru,

u got me.. pls give solution also

HasC 05-26-2010 01:26 PM

you're right Simon, it could be done on just one line :laugh:
OP: man ping :maniacal laugh:

Simon Bridge 05-28-2010 01:18 AM

Quote:

u got me... pls give solution also
open a terminal and enter:

man ping

read through the options.

I got to be a guru because the people who helped me out here did not spoon-feed me answers. I am paying that favour forward. Note: I know how you feel - it is frustrating. However, it is the only way you will learn.

I am happy to provide the actual answer though, if you can demonstrate that you have read the manual page and are still stuck. Reading the manual is the most fundamental skill for you to learn. In addition, I have all-but spelled it out in post #8.

Have a look at the words I used there and compare with the words used in the manual. Seriously, if you are unable to figure this out, then you have a bad problem.

BTW: It is "you" not "u" and "please" not "pls". Are you really posting from a cell phone? This sort of writing just makes you look lazy - and your continued refusal to look at the manual only confirms this. If you can't be bothered even to write whole words, why would you expect anyone else to bother to write whole answers?


All times are GMT -5. The time now is 03:10 AM.