Jon said:
In /etc/rc.d/... there usually resides a bunch of other init scripts
that can be used as a starting point. Also search for "jboss init
script" or "tomcat init script" or something.
The scripts are all in /etc/rc.d/init.d. As a shortcut, some distros
(Fedora for one) provide a shortcut by also creating the hard link
/etc/init.d.
When you've copied a script and modified it to launch your server you
need to set up its run levels - usually you'll want it to run at levels
3 (network active), 4 (not usually used) and 5 (X-terminal active). You
set its run levels with the chkconfig command.
When the script is installed and working you can control it manually if
you login as root and use the service command (, e.g. "service myscript
status").
There's one thing to note: a server becomes a daemon when its parent has
exited. In C the server contains code something like:
reply = fork();
if (reply == 0)
{
/* If you got here you are the child process, so initialize the
server and carry out the its tasks
*/
else
{
/* If you got here you are the forking process, which
simply terminates so its child is a daemon
*/
exit(0);
}
This is needed if your launching script is to terminate: if you try to
start the server with "myserver &" your script will run to completion
but will then hang until all its children, i.e. your server, terminate
and this is NOT what you what. The C fragment above avoids that because
it duplicates the process containing it as a child and then terminates.
The result is that the child becomes a daemon (its parent is dead) and
the script will exit because all its children have terminated.
However, Java can't do the fork() trick (there's no fork() method in the
System class) but there is a shell script function that can do it for
you called "daemon". Pick on a script that uses it and modify that one.
The "crond" script looks like a good starting point.
Most linux distros use Sys V style initscripts (slacware is an exception
I think). They don't differ much from distro to distro in functionality,
meaning they accept start, stop and restart parameter, but the actual
implementation may differ.
NOTE: starting your server this way is best for servers that are slow to
start and/or are almost constantly in use. If your server starts up fast
(i.e. in under a second) and is only rarely used, you might consider
starting it with the xinetd "super server". This way the server is
started when a client connects to it and it stops when the (last)
connection is closed. In practice the used can't tell whether a server
was started at boot time or via xinetd. For example sshd is usually
started at boot time but ftpd is normally started on demand by xinetd.