Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I use logger in a shell script?

If I run

ntpd -qnp 2> >(logger)

in console, it works as expected.

If I put it into a shell script

ntpd -qnp 2> >(logger)

I get following error:

line 2: syntax error: unexpected redirection
exited with code 2

What is wrong in the shell script?

share|improve this question
Can you tellwhat you are trying to achieve, and which linux distro you are using please? – user130370 Dec 28 '13 at 12:45
I am using busybox and want to log the ntpd output, which will run as cron. – user1091344 Dec 28 '13 at 13:30
up vote 6 down vote accepted

The shebang is correct as both #!/bin/sh and #!/bin/bash can be used*, however, IIRC, you should use pipes not redirect for logger as it's a program not a file.

Try this script:

ntpd -qnp 2>&1 |logger

*If you use #!/bin/sh for your scripts, they are supposed to be posix compliant, ie run well on solaris and AIX. If ever you use some bash specific features, #!/bin/bash have to be used because there's no more portability.

share|improve this answer
I tried the pipe solution, but the output is not logged in /var/log/messages but printed to the console. – user1091344 Dec 28 '13 at 11:35
Replace ntpd -qnp by echo "test" and you'll figure out if the second problem comes from logger. Be sure to check ntpd man page as setting as pidfile is not likely to work like you expect I guess. – user130370 Dec 28 '13 at 12:09
ntpd prints to stderr, so, "ntpd -dnq -p 2>&1 |logger" does the trick :-) thank you! – user1091344 Dec 28 '13 at 13:29

What's wrong is that you're using bash-ism's while your hash-bang is saying /bin/sh. Change it to #! /bin/bash and it will work.

share|improve this answer
That should rarely be a problem. Are there actually any systems which enforce "sh-ness" if they see "#!/bin/sh"? Note: Trying a bashism in a script flagged with "#!/bin/sh" on Linux works if you run it as "./" as the default shell (bash) is used but not if your run it as "sh" (Manual: "If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well.") – David Tonhofer Dec 28 '13 at 16:27

I actually had to check what the line ntpd -qnp 2> >(logger) does:

  1. Run ntpd -qnp
  2. Redirect its stderr using 2> to ... where exactly? Well....
  3. >(logger)
    1. Creates a named pipe (see Mechanism), call it /dev/fd/pipe100
    2. Starts process logger reading its STDIN from that pipe
    3. Substitutes the name /dev/fd/pipe100 into the command
  4. ...thus running ntpd -qnp 2> /dev/fd/pipe100
  5. ...while process logger reads from that pipe: logger < /dev/fd/pipe100

The above should work nicely in bash.

However, I tried to run it using "sh" explicitly, which yields: line 3: syntax error near unexpected token `>'

So, as the other commenters noted, you are probably not using the bash here.

Indeed, Busybox uses the Almquist Shell by default.

share|improve this answer

Most likely you have a different shell with /bin/sh than your default shell (probably bash)

To check try to run the script with /bin/sh ./yourscript or put the same shell as $SHELL into your script

Which shell is used for /bin/sh is probably distribution dependent plus if you are on Solaris, it is something completely different.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.