I've got this strange behaviour Empty string parsing ntpq command result, but let me resume and refocus the problem:
I'm executing a java program launched using a shell script that goes like this:
#!/bin/bash
export PATH=.:$PATH
java -jar myJar.jar &
Inside my java code I execute this piped command
ntpq -c peers | awk ' $0 ~ /^\*/ {print $9}'
in order to obtain the offset of the NTP synchronized server.
To execute a piped command inside a java program, I've to use the above mentioned line as an argument of /bin/sh. I execute /bin/sh not the piped command directly.
This is the equivalent that you can launch in a console 1
/bin/sh -c 'ntpq -c peers | awk '"'"' $0 ~ /^\*/ {print $9}'"'"''
Example output from ntpq
remote refid st t when poll reach delay offset jitter
==============================================================================
*172.30.100.1 172.22.204.171 4 u 207 1024 377 1.490 53.388 49.372
Parsing it with awk
, I obtain
53.388
Usually it goes well, but sometimes for reasons unknown [This is my question] my program stops working fine. It returns nothing when the execution of the piped command from the console returns a number.
Recovering the err from the executed process created by java, I've obtained this text
/bin/sh: ntpq: command not found
So, sometimes I can execute 1 from a java program and sometimes I can't. Something is happening in the SO behind the scene. Can someone enlighten me, please?
ntpq
binary? Does this cause issues?/bin/sh -c '/usr/sbin/ntpq -c peers | awk '"'"' $0 ~ /^\*/ {print $9}'"'"''
from my java code? I've just tried and it works. But my original solution works too until it doesn't. I don't know how to force the anomalous situation. How can I proof that yours is better? :-(ntpq
not always return the expected result. For example, I just ranntpq -c peers
and I get things like*46.243.26.34 (4 .GPS. 1 u 15 64 3 22.912 -1.441 4.577
As you can seecolumn 9
isn't the offset anymore. Also are you exporting JAVA as well?