3

Ubuntu 12.04 Server x64, Varnish 3.0.2

I'm trying to pipe varnishncsa's logs through Apache's rotatelogs, and running from the shell, things work fine:

sudo varnishncsa -a -P /var/run/varnishncsa/varnishncsa.pid |/usr/sbin/rotatelogs /var/log/varnish/varnish.log.%Y%m%d%H 3600

creates a new logfile in /var/log/varnish, with rotation every hour (3600 seconds). However, I'm struggling to get things working the same way inside /etc/init.d/varnishncsa:

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/$NAME
PIDFILE=/var/run/$NAME/$NAME.pid
LOGFILE=/var/log/varnish/varnishncsa.log
USER=varnishlog

DAEMON_OPTS="-a -P ${PIDFILE}"
DAEMON_PIPE="|/usr/sbin/rotatelogs /var/log/varnish/varnish.log.%Y%m%d%H 3600"

...

start_varnishncsa() {
    output=$(/bin/tempfile -s.varnish)
    log_daemon_msg "Starting $DESC" "$NAME"
    create_pid_directory
    if start-stop-daemon --start --verbose --pidfile ${PIDFILE} \
        --chuid $USER --exec ${DAEMON} -- ${DAEMON_OPTS} \
        > ${output} 2>&1; then
        log_end_msg 0
    else
        log_end_msg 1
        cat $output
        exit 1
    fi
    rm $output
}

Where should I put DAEMON_PIPE in the above code? I've tried at the end of:

if start-stop-daemon --start --verbose --pidfile ${PIDFILE}

which is where additional command line parameters usually go, but it isn't creating a logfile.

1

You could modify /etc/init.d/varnishncsa in the following way:

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/$NAME
PIDFILE=/var/run/$NAME/$NAME.pid
LOGFILE=/var/log/varnish/varnishncsa.log
USER=varnishlog
DAEMON_OPTS="-a -P $PIDFILE"

...

start_varnishncsa() {
    output=$(/bin/tempfile -s.varnish)
    log_daemon_msg "Starting $DESC" "$NAME"
    create_pid_directory
    if start-stop-daemon --start --quiet --pidfile ${PIDFILE} \
        --chuid $USER -b --exec /bin/bash \
        -- -c "${DAEMON} ${DAEMON_OPTS} \
               | /usr/sbin/rotatelogs /var/log/varnish/varnish.log.%Y%m%d%H 3600" \
        > ${output} 2>&1; then
    log_end_msg 0
    else
    log_end_msg 1
    cat $output
    exit 1
    fi
    rm $output
}

This happens: start-stop-daemon spawns a bash, which executes varnishlog with logging to stdout and pipes output through rotatelogs. The -b parameter lets start-stop-daemon detach the started process to background. The downside is, that start-stop-daemon can not detect anymore, if the command executes successfully.

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.