Musings about Coding, Business and other Geek Stuff Live and Direct from somewhere on the planet
December 03, 2002
Using daemontools for Orion and Postgresql

One thing I always used to hate about linux was SysV style init scripts.
I always wondered why they had to be so complicated. I would also always do something stupid so when a server restarted a service wouldn't actually restart. Also what happens when a service dies? (Basically nothing, unless you have other monitoring software)
Apache had a pretty architecture early on with a signle supervise process managing several worker processes.
This is similar to D.J. Bernstein's approach to services in daemontools. For some of the benefits read his faq. So from now on just trust me this is what you want if you're running daemons of various flavors on unix.
The process of using daemontools for other of DJB's software such as qmail and tinydns is pretty well documented. But there isn't much describing how you use it with other server software. This is what I'm planning to write about here.

The basic concept of installing a service under daemontools is quite simple. I'm assuming you've already installed daemontools under /service.
You basically create a new directory for your service. Dont do this under /service just yet though. So for Orion you could create a directory called daemontools in your orion directory.
Within that directory you create a new file called run. This file is just a simple shell script that runs your daemon in foreground mode. eg.

#!/bin/sh
cd /usr/local/java/orion
exec java -jar orion.jar

Make sure you make this file executable.
This is very barebones of course. The server would run as root.
To install the service you simply link the directory you created before into your /service directory. eg:
ln -s /usr/local/java/orion/daemontools /service/orion
Thats it. Try doing a ps auwx and you should see the orion java process running as well as process called supervise orion.
This supervise process is what manages the lifecycle of your service.
You can control it with the svc command. See the docs for more info. But the basic commands that you need are:
svc -t /service/orion - Sends the service a TERM signal and restarts it.
svc -d /service/orion - Brings the service down.
svc -u /service/orion - Brings the service up.
To see why daemon tools are cool try killing the orion process manually. If you proceed to do a ps auwx you will notice that it's been restarted. Aint that just the coolest.
To run Orion as a different user. Create the user eg. orion and add the setuidgid command before java. eg:
exec setuidgid orion java -jar orion.jar
To restart it do a "svc -t /service/orion"
As a final touch we'll add support for daemontools log mechanisms.
DJB normally recommends having a different user for the log process for security reasons. So create a user called "orionlog".
within your /service/orion directory create a new directory called "log" and a directory called "log/main". Chown log/main to the orionlog user and create a new file called "log/run" with the following content:
#!/bin/sh
exec setuidgid orionlog multilog t ./main

Make sure this file is executable.
Also modify your original /service/orion/run file by adding this line before the java process line:
exec 2>&1. This reroutes STDERR to STDOUT.
Finally restart it as before. You now have orion running well under daemontools.
The daemontools logs will be in /service/orion/log/main/current

To setup Postgresql create the same basic directory structure with the following as the run file:


#!/bin/sh
echo Starting PostgreSQL
exec 2>&1
exec setuidgid postgres /usr/local/pgsql/bin/postmaster -D /usr/local/pgsql/data

Use the same log/run file as for orion just change the username to postgreslog (and remember to add that as a os user). Then link it into the /service directory as /service/postgres.

Posted by pelleb at December 03, 2002 03:18 PM
This entry was posted in the following Categories: Java , Open Source
Comments

At atlassian we use daemontools to run Orion and postgres also.

I also like to know how often the server is restarted, so I put that in my run script. My run script looks like this:

#!/bin/sh

# latest runline
cd /opt/orion

echo `date` >> /opt/orion/log/restart.log
exec \
setuidgid orion \
java -server -Xms150m -Xmx450m -jar orion.jar -out log/stdout.log -err log/stderr.log

If you decide not to use daemontools - use this script instead:

http://kb.atlassian.com/content/orionsupport/articles/unixprocess.html

Cheers,
Scott

Posted by: Scott Farquhar on December 3, 2002 11:54 PM

Thats a cool idea. I'll add that to my script.

Posted by: Pelle on December 4, 2002 04:52 AM

This is one for Your log.

#!/bin/sh
#
# PostgreSQL run script for daemontools v1.0
# Author: DEBO Jurgen
# Date : 25 Nov 2003
#
logfile=/var/log/pgsql
exec 2>&1
touch ${logfile} && chmod 600 ${logfile} && chown postgres:postgres ${logfile}
echo `date`" : Daemontools runs PostgreSQL." >> ${logfile}
exec setuidgid postgres /usr/local/pgsql/bin/postmaster -D /usr/local/pgsql/data >> ${logfile}

Posted by: DEBO Jurgen on November 26, 2003 04:45 AM
Post a comment
Name:


Email Address:


URL:


Comments:


Remember info?