0

I ran into notes for a server I used to work on back in the day but the redirection does not look right. Maybe someone can confim?

These were entries in my crontab entered by a third-party vendor who maintained the app/database that ran in this HP-UX server.

15 21 * * 0-6 /usr/sys/bin/stop  q  >/stopout   2<&1 #
30 21 * * 0-6 /usr/sys/force     q  >/out       2<&1 #
55 23 * * 0-6 /usr/sys/start     q  >/startout  2<&1 #

I found this in the Bash manual but this means that stderr and stdout is going out to >/{stopout,out,startout} while stderr is copying stdout (which means stdout is twice being written to stopout,out,startout?)? I'm confused :)

3.6.8 Duplicating File Descriptors
The redirection operator

[n]<&word
is used to duplicate input file descriptors. If word expands to one or more 
digits, the file descriptor denoted by n is made to be a copy of that file 
descriptor. If the digits in word do not specify a file descriptor open for 
input, a redirection error occurs. If word evaluates to ‘-’, file descriptor n 
is closed. If n is not specified, the standard input (file descriptor 0) is 
used.
2

It's highly likely to be a typo for 2>&1; >foo 2>&1 is very common in crontabs as a simple way of keeping the output and error of cronjobs (see SO, U&L, AU, ...).


Note that:

  • fd 1 here has been opened for writing (>/stopout, etc. equivalent to 1>/stopout)
  • whether you do 2>&1 or 2<&1, fd 2 gets dup2'd (at least on Linux, but presumably on other platforms as well) to fd 1, so it can be used in the same ways as fd 1 can be.
    • This does not mean that data written to fd 1 will be sent to fd 2 as input, but that fds 1 and 2 point to the same thing.
  • so writing to fd 2 will not fail, and is sent to /stopout just like any data written to fd 1.

However, if fd 1 was originally opened for reading, writing to either would fail:

$ strace -e write bash -c 'echo bar 1<foo 2<&1'
write(1, "bar\n", 4)                    = -1 EBADF (Bad file descriptor)
write(2, "bash: line 0: echo: write error:"..., 53) = -1 EBADF (Bad file descriptor)
+++ exited with 1 +++

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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