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

I'm trying to write init config that will redirect output of my daemon to two files (for stdout and stderr). The problem is, it's not working. I'm reading this right now.

So, I've done this shell script to test this approach. And it doesnt' work:

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="Seed kurokikaze starter"
#NAME=node
DAEMON="/etc/node-version/0.1.99/bin/node"
DAEMON_ARGS="/etc/seed/kurokikaze/server.js"
DAEMON_DIR="/etc/seed/kurokikaze"

APPLOG_FILE="/var/log/seed/kurokikaze"
ERRLOG_FILE="/var/log/seed/kurokikaze-err"

PIDFILE="/var/run/seeds/kurokikaze.pid"
SCRIPTNAME="/etc/seed-init/kurokikaze"

NCMD="exec $DAEMON $DAEMON_ARGS 1>>$APPLOG_FILE 2>>$ERRLOG_FILE"

start-stop-daemon -Sbmv --pidfile $PIDFILE --chdir ${DAEMON_DIR} --exec $DAEMON --startas /bin/sh -- $NCMD

But if you start this without wrapping daemon in separate shell, it works as intended (just without stderr/stdout redirects):

start-stop-daemon -Sbmv --pidfile $PIDFILE --chdir ${DAEMON_DIR} --exec $DAEMON -- ${DAEMON_ARGS}

The question is: why first script doesn't work? System is Debian Lenny, start-stop-daemon version is 1.14.29

share|improve this question

3 Answers 3

up vote 5 down vote accepted

Because of the order of expansion, you can't pass redirections in variables. Redirection is evaluated before word splitting.

See BashFAQ/050, Shell Expansions and Redirections.

share|improve this answer
    
Yep, so just replace the last two lines with: NCMD="exec $DAEMON $DAEMON_ARGS";start-stop-daemon -Sbmv --pidfile $PIDFILE --chdir ${DAEMON_DIR} --exec $DAEMON --startas /bin/sh -- $NCMD 1>>$APPLOG_FILE 2>>$ERRLOG_FILE –  BMDan Jul 21 '10 at 13:46
    
Will it redirect node streams or start-stop-daemon's? –  Kuroki Kaze Jul 21 '10 at 14:49
1  
@Kuroki and @BMDan: That will redirect the output of start-stop-daemon. –  Dennis Williamson Jul 21 '10 at 21:07
2  
Any way to listen only for daemon output (other of sh wrapper)? –  Kuroki Kaze Jul 22 '10 at 10:13

Your line in the script is:

start-stop-daemon -Sbmv --pidfile $PIDFILE --chdir ${DAEMON_DIR} --exec $DAEMON --startas /bin/sh -- $NCMD

So you are basically saying to exec $DAEMON using $NCMD as args. Try to change $NCMD to

"$DAEMON_ARGS 1>>$APPLOG_FILE 2>>$ERRLOG_FILE"

and see if it works.

If not, you will have to change your software to redirect stdout and stderr to files (maybe it already has a logging option). You can also write a wrapper script just to do the redirection but that is a little on the ugly side.

share|improve this answer

Another way for that streams redirection using of nohup like:

nohup ./bin/servicemix ./conf/servicemix.xml > log/nohup.log 2>&1 &
share|improve this answer

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.