LinuxQuestions.org
Visit Jeremy's Blog.
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 04-13-2017, 12:12 PM   #1
ran4keli
LQ Newbie
 
Registered: Oct 2016
Posts: 2

Rep: Reputation: Disabled
Unhappy Ping device or device2 and log it to a file with timestamp


I have a server, running Centos 6.2, that runs an app that is dependent on a stratus0 timeserver. I need to verify connectivity with a timeserver prior to starting NTPD and the app.
I am currently using the following cmd to accomplish this:
until /bin/ping -c 1 timeserver1 >> /var/log/ntpd.log || /bin/ping -c 1 timeserver2 >> /var/log/ntpd.log ; do
sleep 2
done

The only problem is that I cannot determine the timestamp for the log entries. I have attempted using the following cmd:
/bin/ping -c 1 timeserver1 | xargs -n1 -i bash -c 'echo `date +%F\ %T`" {}"' >> /var/log/ntpd.log || /bin/ping -c 1 timeserver2 | xargs -n1 -i bash -c 'echo `date +%F\ %T`" {}"' >> /var/log/ntpd.log ; do
sleep 2
done

The problem with this is if timserver1 is offline, it only logs the ping failure to timeserver1 and does not perform the or (||) and look for timeserver2.

If this is the wrong forum, please enlighten me and I will post it in the correct forum.

Thank you for your assistance.
 
Old 04-13-2017, 06:00 PM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,844

Rep: Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478Reputation: 1478
It has been a while...

Try putting () around the first command with the pipe. The opening "(" would go before the first ping command. The ")" would go just before the ||

If I remember right, the shell waits for the second command for the exit status and it may be losing the ping exit.
 
Old 04-13-2017, 09:02 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 19,554

Rep: Reputation: 3146Reputation: 3146Reputation: 3146Reputation: 3146Reputation: 3146Reputation: 3146Reputation: 3146Reputation: 3146Reputation: 3146Reputation: 3146Reputation: 3146
Probably not the most efficient. I didn't check your xargs statement.

Code:
ping -c 1 timeserver1 | xargs -n1 -i bash -c 'echo `date +%F\ %T`" {}"' >> /var/log/ntpd.log
a=$?
ping -c 1 timerserver2 | xargs -n1 -i bash -c 'echo `date +%F\ %T`" {}"' >> /var/log/ntpd.log
b=$?
until [[ $a -ne 1 || $b -ne 1 ]]
do
  sleep 2
  ping -c 1 timerserver1 | xargs -n1 -i bash -c 'echo `date +%F\ %T`" {}"' >> /var/log/ntpd.log
  a=$?
  ping -c 1 timerserver2 | xargs -n1 -i bash -c 'echo `date +%F\ %T`" {}"' >> /var/log/ntpd.log
  b=$?
done

Last edited by michaelk; 04-13-2017 at 09:05 PM.
 
  


Reply


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
[SOLVED] How to write ping result in a log file/text file ablopera Linux - Newbie 3 07-13-2015 07:56 AM
[SOLVED] log bash script output to file with timestamp daisychick Programming 11 05-08-2013 02:50 AM
Ping Timestamp tsprespec calculation vino87 Linux - Networking 0 07-02-2009 01:11 AM
Ping & timestamp Zeno McDohl Linux - Newbie 4 09-22-2008 03:24 PM
Simple question about command syntax ping/log ping results ohalnet Linux - Networking 1 07-25-2006 05:46 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:20 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration