Stdout/stderr redirects in start-stop-daemon
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
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.
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.