8

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

3 Answers 3

4

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.

4
  • 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, 2010 at 13:46
  • Will it redirect node streams or start-stop-daemon's? Jul 21, 2010 at 14:49
  • 1
    @Kuroki and @BMDan: That will redirect the output of start-stop-daemon. Jul 21, 2010 at 21:07
  • 2
    Any way to listen only for daemon output (other of sh wrapper)? Jul 22, 2010 at 10:13
1

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.

1

Another way for that streams redirection using of nohup like:

nohup ./bin/servicemix ./conf/servicemix.xml > log/nohup.log 2>&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.