LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 07-14-2008, 03:32 PM   #1
goncalopp
Member
 
Registered: Jun 2008
Posts: 47

Rep: Reputation: Disabled
ping shell script


Hey,
I'm trying to write a shell script to check when my server goes offline (I'm running it on dsl...). Here's the code:

Code:
ping google.com -i 5 -w 5 > /var/log/tmpcheckconnection 2>&1
grep unknown /var/log/tmpcheckconnection > /var/log/tmpcheckconnection2
grep '0 received' /var/log/tmpcheckconnection >> /var/log/tmpcheckconnection2
if [ -s tmpcheckconnection2 ]
then
date '+Connection Unnavailable at %d/%m/%y, %H:%M:%S' >> /var/log/checkconnection.log
fi

rm /var/log/tmpcheckconnection /var/log/tmpcheckconnection2
The thing is, the file checkconnection.log is never created, even when the ping does not succeed :S Any Ideas? (Yes, I'm running it as root)

Also, I plan on running this with cron, and I've put it on my home folder, though only root has rwx access to it; is that ok, or i should put it in another directory? Maybe I shouldn't use /var/log at all??
 
Old 07-14-2008, 04:21 PM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,753
Blog Entries: 1

Rep: Reputation: 238Reputation: 238Reputation: 238
Code:
if [ -s tmpcheckconnection2 ]
then
date '+Connection Unnavailable at %d/%m/%y, %H:%M:%S' >> /var/log/checkconnection.log
fi
Your conditional is checking to see if tmpcheckconnection2 is zero size. Even if the ping is unsuccessful, you will create tmpcheckconnection2 with information (i.e., tmpcheckconnection2 will never be zero size).

Take a look at ping's man page, especially this part
Quote:
If ping does not receive any reply packets at all it will exit with code 1. If a packet count and deadline are both specified, and fewer than count packets are received by the time the deadline has arrived, it will also exit with code 1. On other error it exits with code 2. Otherwise it exits with code 0. This makes it possible to use the exit code to see if a host is alive or not.
Why don't you try to use the exit code from your ping command in the conditional instead of all the temporary files? Something like
Code:
ping -i 5 -w 5
if [ 'x$?' == 'x1' ]; then
     date '+Connection Unnavailable at %d/%m/%y, %H:%M:%S' >> /var/log/checkconnection.log
fi
/var/log is fine for log files...that's what it's for anyway. Depending on how your distro implements cron, there may be /etc/cron.daily, /etc/cron.weekly, etc. directories on your system. Placing the script in one of these directories will cause it to be executed at the appropriate periodicity. Otherwise keeping it in your $HOME is OK too.

Last edited by weibullguy; 07-14-2008 at 04:23 PM.
 
Old 07-14-2008, 04:25 PM   #3
williebens
Member
 
Registered: Jan 2008
Posts: 88

Rep: Reputation: 16
Hello:

Leaving the file in you home directory should work with cron.
I have this setup:

01 1 23 * * root /home/willie/bin/file.sh

It runs well.
Thanks.
--Willie
 
Old 07-14-2008, 05:48 PM   #4
goncalopp
Member
 
Registered: Jun 2008
Posts: 47

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by weibullguy View Post
Your conditional is checking to see if tmpcheckconnection2 is zero size. Even if the ping is unsuccessful, you will create tmpcheckconnection2 with information (i.e., tmpcheckconnection2 will never be zero size).
Acording to the bash man page,
Quote:
-s file
True if file exists and has a size greater than zero.
I still can't spot what i'm doing wrong, but checking the exit code is indeed much more elegant! I had no idea you could do that (I know, i'll take the time to fully read a scripting tutorial one of these days...:P )
I guess I'll just leave the script on my home folder, then
many thanks
 
Old 07-14-2008, 07:12 PM   #5
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,753
Blog Entries: 1

Rep: Reputation: 238Reputation: 238Reputation: 238
Yup, you're right about the -s ... I read it wrong. I wasn't on a Linux box earlier, but I just hacked this together and it seems to work.
Code:
ping -c 5 www.google.com
if [[ $? != 0 ]]; then
    date '+%Y-%m-%d %H:%M:%S Connection Unavailable' >> /var/log/checkconnection.log
else
    date '+%Y-%m-%d %H:%M:%S Connection Available' >> /var/log/checkconnection.log
fi
Gives you a log file that looks like
Code:
2008-07-14 19:09:44 Connection Available
2008-07-14 19:10:11 Connection Available
2008-07-14 19:11:42 Connection Unavailable
 
Old 07-15-2008, 12:53 AM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Just make sure tha if you create a file in a certain dir, you then inc that path in the
if [[ -s /path/file ]]
test. The OP doesn't do that, so it'd only work if the script was running in /var/log....
 
  


Reply

Tags
permissions, ping, shell script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 07:24 AM
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 03:14 AM
I made a shortcut to a shell script and it is using default shell icon... shlinux Linux - Software 2 04-20-2006 07:29 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 03:34 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM


All times are GMT -5. The time now is 11:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration