Java as Daemon in Linux

Discussion in 'Java' started by dimitrik107@hotmail.com, Aug 25, 2006.

  1. Guest

    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
    , Aug 25, 2006
    #1
    1. Advertising

  2. Matt Rose Guest

    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
    Matt Rose, Aug 25, 2006
    #2
    1. Advertising

  3. Matt Rose wrote:
    > 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.
    Jon Martin Solaas, Aug 25, 2006
    #3
  4. Jon Martin Solaas wrote:
    > Matt Rose wrote:
    >> 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 |
    Martin Gregorie, Aug 26, 2006
    #4
  5. 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...
    Jon Martin Solaas, Aug 27, 2006
    #5
  6. 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 |
    Martin Gregorie, Aug 27, 2006
    #6
  7. Matt Rose Guest

    Martin Gregorie wrote:
    > Jon Martin Solaas wrote:
    > > Matt Rose wrote:
    > >> 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
    Matt Rose, Aug 29, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steph L
    Replies:
    2
    Views:
    883
    Alan Coopersmith
    Oct 28, 2003
  2. Philippe C. Martin

    SCLOGON 0.1 Smart Card event daemon for GNU/Linux

    Philippe C. Martin, Nov 5, 2005, in forum: Python
    Replies:
    0
    Views:
    304
    Philippe C. Martin
    Nov 5, 2005
  3. Jean-Paul Calderone
    Replies:
    0
    Views:
    451
    Jean-Paul Calderone
    Mar 20, 2009
  4. Floris Bruynooghe
    Replies:
    1
    Views:
    448
    Floris Bruynooghe
    Mar 24, 2009
  5. Daemon Win32::Daemon;

    , Sep 7, 2006, in forum: Perl Misc
    Replies:
    0
    Views:
    249
Loading...

Share This Page