Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Java as Daemon in Linux

Reply
Thread Tools

Java as Daemon in Linux

 
 
dimitrik107@hotmail.com
Guest
Posts: n/a
 
      08-25-2006
I have java server process that I need to run as daemon in Linux 2.6.18


I need it to start when system boots up. I think I need to place the
start script in /etc/init/.

Can you share your script If you have done something like this?

I am using java 1.5.0_07 on Linux 2.6.18

 
Reply With Quote
 
 
 
 
Matt Rose
Guest
Posts: n/a
 
      08-25-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> I have java server process that I need to run as daemon in Linux 2.6.18
>
>
> I need it to start when system boots up. I think I need to place the
> start script in /etc/init/.
>
> Can you share your script If you have done something like this?
>
> I am using java 1.5.0_07 on Linux 2.6.18


It depends which distribution of linux you are using. Linux itself is
just the kernel at the heart of the system. The other ten thousand
programs installed when you install "linux" come from all sorts of
places (including gnu) and are collected together into a usable
operating system by the distribution provider (for example Debian or
Red Hat).

The method used for launching and controlling daemons varies from
distribution to distribution.

Matt

 
Reply With Quote
 
 
 
 
Jon Martin Solaas
Guest
Posts: n/a
 
      08-25-2006
Matt Rose wrote:
> (E-Mail Removed) wrote:
>> I have java server process that I need to run as daemon in Linux 2.6.18
>>
>>
>> I need it to start when system boots up. I think I need to place the
>> start script in /etc/init/.
>>
>> Can you share your script If you have done something like this?
>>
>> I am using java 1.5.0_07 on Linux 2.6.18

>
> It depends which distribution of linux you are using. Linux itself is
> just the kernel at the heart of the system. The other ten thousand
> programs installed when you install "linux" come from all sorts of
> places (including gnu) and are collected together into a usable
> operating system by the distribution provider (for example Debian or
> Red Hat).
>
> The method used for launching and controlling daemons varies from
> distribution to distribution.
>
> Matt
>


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.

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.
 
Reply With Quote
 
Martin Gregorie
Guest
Posts: n/a
 
      08-26-2006
Jon Martin Solaas wrote:
> Matt Rose wrote:
>> (E-Mail Removed) wrote:
>>> I have java server process that I need to run as daemon in Linux 2.6.18
>>>
>>>

> 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.


--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
 
Reply With Quote
 
Jon Martin Solaas
Guest
Posts: n/a
 
      08-27-2006
Martin Gregorie wrote:
>
> 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.


Not sure if the JVM is suitable for this approach. It's fast as anything
once it's up and running, but it takes a while getting there... but it
all depends on load / request frequency and stuff. And maybe the java
code even is compiled to native executables... still it's kind of
similar to the old cgi wep-app interface...
 
Reply With Quote
 
Martin Gregorie
Guest
Posts: n/a
 
      08-27-2006
Jon Martin Solaas wrote:
> Martin Gregorie wrote:
>> 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.

>
> Not sure if the JVM is suitable for this approach. It's fast as anything
> once it's up and running, but it takes a while getting there... but it
> all depends on load / request frequency and stuff. And maybe the java
> code even is compiled to native executables... still it's kind of
> similar to the old cgi wep-app interface...
>

Agreed. I was in anal completist didactic mode when I wrote that.

Nothing I own would be fast enough to give an acceptable startup time,
but I only have a 766 MHz box with 256 MB RAM and little idea of how
fast a state of the art system might manage to start an
xinetd-controlled server.

--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
 
Reply With Quote
 
Matt Rose
Guest
Posts: n/a
 
      08-29-2006

Martin Gregorie wrote:
> Jon Martin Solaas wrote:
> > Matt Rose wrote:
> >> (E-Mail Removed) wrote:
> >>> I have java server process that I need to run as daemon in Linux 2.6.18
> >>>
> >>>

> > 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").
>


This is sound advice if you are on a Redhat derived system. The details
are slightly different on Debian derived systems. The run levels are
usually not used much: 1 means single user (i.e. for unusual
maintenance), 2 means normal. 3, 4 and 5 are not used by default,
although you're welcome to change this if you have a need. Also, the
scripts live in /etc/init.d. The /etc/rc.d symlink farm is maintained
using 'update-rc.d' instead of 'chkconfig' and services can be started
and stopped using 'invoke-rc.d' (or just by invoking the script
directly if you're just experimenting) instead of 'service'.

A good init script can cope with quite a few exceptional conditions,
many of which will vary according to the distro you are running on
(e.g. where should you store the pid file, where should you check for
java's existance and so on). I recommend you get hold of something like
tomcat packaged up for your particular system and have a look at the
init scripts that it comes with.

Matt

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: PEP 3143: Standard daemon process library (was: Writing awell-behaved daemon) Floris Bruynooghe Python 1 03-24-2009 02:58 PM
Re: PEP 3143: Standard daemon process library (was: Writing awell-behaved daemon) Jean-Paul Calderone Python 0 03-20-2009 01:02 PM
Daemon Win32::Daemon; ph1975@gmail.com Perl Misc 0 09-07-2006 10:58 AM
SCLOGON 0.1 Smart Card event daemon for GNU/Linux Philippe C. Martin Python 0 11-05-2005 05:31 PM
Porting from OpenBSD/Linux to Solaris : daemon(3) Steph L C Programming 2 10-28-2003 06:57 AM



Advertisments