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:

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
*   4 u  207 1024  377    1.490   53.388  49.372

Parsing it with awk, I obtain


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?

  • 1
    Have you tried to pass the full path to the ntpq binary? Does this cause issues? Oct 20, 2017 at 10:04
  • @val0x00ff You mean executing /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? :-(
    – RubioRic
    Oct 20, 2017 at 10:17
  • This could be caused by ntpq not always return the expected result. For example, I just ran ntpq -c peers and I get things like * (4 .GPS. 1 u 15 64 3 22.912 -1.441 4.577 As you can see column 9 isn't the offset anymore. Also are you exporting JAVA as well? Oct 20, 2017 at 10:22
  • @val0x00ff So you have a different SO or a different ntpq implementation, haven´t you? Anyway, it doesn`t explain why sometimes my SO found ntpq command and sometimes doesn't. Thanx for your comments :-)
    – RubioRic
    Oct 20, 2017 at 10:41

1 Answer 1


I originally posted a similar question in StackOverflow, thinking that the problem may be related with the java programming but it wasn't.

Finally we found what was happening.

My java program is launched with a shell script. When we execute the script manually, ntpq command is found and invoked successfully. The problem arises when the software is fully deployed. In the final environment we've got a cron scheduled demon that keeps our program alive but PATH established by cron is different from the PATH that our profile has got assigned.

PATH used by cron:


PATH that we got login for launching the script manually:


Usually ntpq is in


Here you can find a better description of the problem and various solutions.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .