Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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??
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 03:23 PM.
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
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....
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.