Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How can I use logger in a shell script?

If I run

ntpd -qnp pool.ntp.org 2> >(logger)

in console, it works as expected.

If I put it into a shell script

#!/bin/sh
ntpd -qnp pool.ntp.org 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? –  Eric DANNIELOU 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
add comment

4 Answers

up vote 5 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:


#!/bin/sh
ntpd -qnp pool.ntp.org 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 pool.ntp.org by echo "test" and you'll figure out if the second problem comes from logger. Be sure to check ntpd man page as setting pool.ntp.org as pidfile is not likely to work like you expect I guess. –  Eric DANNIELOU Dec 28 '13 at 12:09
1  
ntpd prints to stderr, so, "ntpd -dnq -p pool.ntp.org 2>&1 |logger" does the trick :-) thank you! –  user1091344 Dec 28 '13 at 13:29
add comment

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 "./myscript.sh" as the default shell (bash) is used but not if your run it as "sh myscript.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
add comment

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

  1. Run ntpd -qnp pool.ntp.org
  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 pool.ntp.org 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:

script.sh: 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
add comment

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
add comment

Your Answer

 
discard

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.