7

I looking into writing my own init.d scripts to control several services running on my Linux server. I came across an example online which contained:

nohup $EXEC_SCRIPT 0<&- &> $LOG_FILE & echo $! > $PID_FILE

From what I understand:

  • nohup Catches the hangup signal
  • $EXEC_SCRIPT is a variable containing the command to be run
  • 0<&- &> Not come across this before
  • $LOG_FILE similar to $EXEC_SCRIPT but contains the log file path
  • & starts logging to $LOG_FILE in the background?
  • $! is the PID of the most last background command
  • > writes the result of $! to the $PID_FILE

I can work through it with this knowledge but the 0<&- &> is completely throwing me off. I don't like to include things that I don't at least partially understand first.

migrated from stackoverflow.com Aug 20 '15 at 15:44

This question came from our site for professional and enthusiast programmers.

8

These are redirections.

  • 0<&- closes the file descriptor 0 (standard input).
  • &> redirects both stdout and stderr (in this case to the logfile)

Are you sure there was no echo before $!? $! would be interpreted as a command and most probably result in a

-bash: 18552: command not found
  • You are correct, there was an echo before $! thanks for the explanation – Chris Edwards Aug 19 '15 at 13:51
2

&<- closes the file descriptor. But more fundamentally, searching for those little "&" atoms on Google can be painful! See this site on bash redirection for background info.

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.