César Ortiz
César Ortiz

Reputation: 311

Getting ntpdate result for scripting

I have two NTP servers with different IPs.

I'm trying to make a bash script that synchronizes the system clock with "ntpdate" client. If first NTP server doesn't respond, my script should try to connect to second NTP server.

I tried to make a system variable called RESULT in this way: RESULT = ntpdate 192.168.100.41

NTP synchronization works, but when I make an "echo $RESULT", its value is always 0. So my question is: Is there a correct way to do this? How?

Upvotes: 3

Views: 2276

Answers (1)

barti_ddu
barti_ddu

Reputation: 10299

Just check execution status stored in $?:

ntpdate foo.com
17 Feb 12:35:13 ntpdate[16218]: no server suitable for synchronization found
echo $?
1

ntpdate 1.europe.pool.ntp.org
17 Feb 12:36:26 ntpdate[16220]: step time server 109.230.243.8 offset 27.014301 sec
echo $?
0

Alternatively, ir you are on Debian, use ntpdate-debian and specify server list in /etc/default/ntpdate, e.g.:

NTPSERVERS="0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org"

Upvotes: 2

Related Questions