Start Trac on Startup – Init.d Script for tracd

As part of a server move, I went on to reinstall Trac. I’ve tried to install it as FastCGI but I failed to configure the clean URLs properly. I got the clean URLs to work if the user access them, but Trac insisted on addeing trac.fcgi to the beginning of every link it generated. So I’ve decided to use the Trac standalone server, tracd.

The next problem I faced was how to start the Trac automatically upon startup. The solution was to use an init.d script for stating Trac. After some searching, I didn’t find an init.d script for tracd that were satisfactory (mostly poorly written). So I went on an wrote my own init.d script for tracd.

#!/bin/sh
### BEGIN INIT INFO
# Provides:          tracd
# Required-Start:    networking
# Required-Stop:     networking
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start the tracd standalone Trac web server.
### END INIT INFO
# (C) 2008 Guy Rutenberg <http://www.guyrutenberg.com>

## Options you should probably change ##
HOSTNAME=127.0.0.1 # to which hostname should we listen
# If you only want to serve one project keep this variable
# empty and set the PROJECT_ENV variable 
ENV_PARENT_DIR=/home/guyru/trac.guyrutenberg.com
PROJECT_ENV=
PORT=9090
# add any additional options (such as authentication) here. If you only have one
# project you should probably add -s here
ADDITIONAL_OPTS=

## Options you should probably not change ##
DAEMON=/usr/bin/tracd
NAME=tracd
DESC="web server"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
SSD="/sbin/start-stop-daemon"

test -x $DAEMON || exit 1

set -e

. /lib/lsb/init-functions

DAEMON_OPTS="--daemonize --pidfile=$PIDFILE --port=$PORT --hostname=$HOSTNAME $ADDITIONAL_OPTS"
if [ -n "$ENV_PARENT_DIR" ]; then
	DAEMON_OPTS="$DAEMON_OPTS --env-parent-dir=$ENV_PARENT_DIR"
else
	DAEMON_OPTS="$DAEMON_OPTS $PROJECT_ENV"
fi

case "$1" in
  start)
	log_daemon_msg "Starting $DESC" $NAME
	if ! $SSD --start --quiet\
	--pidfile $PIDFILE --exec $DAEMON -- $DAEMON_OPTS ; then
            log_end_msg 1
	else
            log_end_msg 0
	fi
    ;;
  stop)
	log_daemon_msg "Stopping $DESC" $NAME
	if $SSD --stop --retry 30\
	--pidfile $PIDFILE ; then
	    rm -f $PIDFILE
	    log_end_msg 0
	else
	    log_end_msg 1
	fi
	;;
  restart|force-reload)
	$0 stop
	[ -r  $PIDFILE ] && while pidof -x $NAME |\
		 grep -q `cat $PIDFILE 2>/dev/null` 2>/dev/null ; do sleep 1; done
	$0 start
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0

The script begins with a configuration section. You should go over each variable and make sure its value suits you need. The first part is configuration variables you are more likely to change. It includes:

  • HOSTNAME – This tells tracd on what hostname to listen. From my experience setting it to anything other than 127.0.0.1 or localhost will cause it to operate on all available hostnames. If you use tracd in conjunction with mod_proxy (this is usually the case if you have Lighttpd) you would want to leave the hostname on 127.0.0.1 so the tracd server won’t be accessed directly.
  • ENV_PARENT_DIR – If you have multiple projects (all under then same directory) set this to the parent directory. If you only want to serve one project you should leave this empty and set the next variable.
  • PROJECT_ENV – Set this to the project environment directory of you project if you serve only one. This variable is ignored if ENV_PARENT_DIR is set to anything but empty.
  • PORT – This simply tells tracd on what port to listen.
  • ADDITIONAL_OPTS – This variable allows you to pass additional parameters to the tracd. It’s the place to add you authentication flags. If you have only a single project and would like to omit the project name in the url (e.g. something like http://trac.example.com) add the -s flag to this variable.

The other part of the configuration allows you to easily adjust the script to your machine. It has variables for specifying the path to the tracd executable and the paths of other things required of the correct operation of the script. In most cases you wouldn’t have to change this part.

Now copy the script to /etc/init.d/trac and adjust the configuration to your needs. To enable the script on startup you should use your distro specific tools for managing the rc scripts. On fedora that means you will have to do chkconfig trac on. On Ubuntu you should do update-rc.d trac defaults.

I hope you will find the script useful. If you have any comments or suggestions regarding the script please comment.

UPDATE (13/10/2008):
Here is an version of the script for download.
UPDATE (17/2/2009):
See Starting tracd without Root Privileges at Startup for a different way to start tracd that doesn’t require root privileges.

16 thoughts on “Start Trac on Startup – Init.d Script for tracd”

  1. Hi, Thank you for the script,
    but i get the following error:

    /etc/init.d/trac: line 30: test: too many arguments
    /etc/init.d/trac: line 39: syntax error near unexpected token `else’

    do you have a tip?
    Thank you!

  2. Hi,

    Apparently I had some escaping issue that caused characters like | and ; not to show up, hence messing up the code. I fixed it and the script should run fine now.

    Thanks for reporting

  3. Thanks for your work, Guy!

    Unfortunately, it fails on FreeBSD (though I understand that most probably you haven’t targeted it): default trac binary location is different, /lib/lsb/init-functions not found, /sbin/start-stop-daemon not found.

    Anyways, why don’t you try to contribute this to Trac-hacks scripts so it appears on community pages? (Trac project itself does not want to host them: http://trac.edgewall.org/ticket/4352)

    Also, it would be nice to have user options (HOSTNAME, ENV_PARENT_DIR, PROJECT_ENV, PORT and ADDITIONAL_OPTS) parametrized, like in this script: http://adrenalinexp.wordpress.com/2008/03/23/freebsd-tracd-rcd-script/

  4. Hi Vitaly,

    I really didn’t think about *BSD when I wrote it (I admit I never even used one).

    Creating a separate configuration file is a nice thing, I just don’t know if it worth the effort for such short script.

  5. Hi Guy ,

    if I normally needs the sudo command to execute the tracd standalone server. Where do I specify this in the script ?

    Thanks

  6. Hi Shredder,

    Init.d scripts are executed with root privileges so no need to use sudo inside the script.

  7. Hi Guy!

    Thanks a lot for your script! I also added a “–chuid trac” to the start-stop-daemon options, which executes tracd as my user “trac”. In this case, the PID file should reside outside /var/run, as the trac user needs access to it.

    Best,
    Ingo

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.