How to install Apache Karaf 3.0.1 as a daemon

apache karaf 3.0.1 as daemon
In this tutorial I show you how you can install Apache Karaf 3.0.1 as a daemon (service) on your linux system.

Download and install Apache Karaf 3.0.1

First go to the location where you want to install Apache Karaf 3.0.1.

$ cd /usr/share

Then download the newest Karaf Binary Distribution

$ sudo wget http://mirror.switch.ch/mirror/apache/dist/karaf/3.0.1/apache-karaf-3.0.1.tar.gz

In the next step we unpack the gz file.

$ sudo tar -zxvf apache-karaf-3.0.1.tar.gz

Now you can start the karaf with

$ cd apache-karaf-3.0.1/bin
$ sudo ./karaf

If you can see the following lines, then karaf started successfully and you can use the karaf console for the next steps.

karaf: JAVA_HOME not set; results may vary
        __ __                  ____
       / //_/____ __________ _/ __/
      / ,<  / __ `/ ___/ __ `/ /_
     / /| |/ /_/ / /  / /_/ / __/
    /_/ |_|\__,_/_/   \__,_/_/

  Apache Karaf (3.0.1)

Hit '' for a list of available commands
and '[cmd] --help' for help on a specific command.
Hit '<ctrl-d>' or type 'system:shutdown' or 'logout' to shutdown Karaf.

karaf@root()>

Now Karaf is running but not as a Linux Daemon, as soon as you leave the Karaf console with

'<ctrl-d>', 'system:shutdown' or 'logout'

the Karaf will stop. In the next step we’ll start Karaf as a Daemon for production use.

Apache Karaf 3.0.1 as a Daemon for production use

Fortunately Karaf has a feature to install Karaf as a deamon.

karaf@root()> feature:install service-wrapper

Afterwards check whether the feature service-wrapper has been installed correctly or not.

karaf@root()> feature:list | grep service-wrapper
service-wrapper               | 3.0.1            | x         | standard-3.0.1          | Provide OS integration (alias to wrapper feature)

If you can see the char x then everything went fine. Now use the new command to install the service wrapper.

karaf@root()> wrapper:install
Creating file: /usr/share/apache-karaf-3.0.1/bin/karaf-wrapper
Creating file: /usr/share/apache-karaf-3.0.1/bin/karaf-service
Creating file: /usr/share/apache-karaf-3.0.1/etc/karaf-wrapper.conf
Creating file: /usr/share/apache-karaf-3.0.1/lib/libwrapper.so
Creating file: /usr/share/apache-karaf-3.0.1/lib/karaf-wrapper.jar
Creating file: /usr/share/apache-karaf-3.0.1/lib/karaf-wrapper-main.jar

Setup complete.  You may wish to tweak the JVM properties in the wrapper configuration file:
        /usr/share/apache-karaf-3.0.1/etc/karaf-wrapper.conf
before installing and starting the service.


RedHat/Fedora/CentOS Linux system detected:
  To install the service:
    $ ln -s /usr/share/apache-karaf-3.0.1/bin/karaf-service /etc/init.d/
    $ chkconfig karaf-service --add

  To start the service when the machine is rebooted:
    $ chkconfig karaf-service on

  To disable starting the service when the machine is rebooted:
    $ chkconfig karaf-service off

  To start the service:
    $ service karaf-service start

  To stop the service:
    $ service karaf-service stop

  To uninstall the service :
    $ chkconfig karaf-service --del
    $ rm /etc/init.d//usr/share/apache-karaf-3.0.1/bin/karaf-service

As written above install the service on Linux. First create the service symbolic link.

sudo ln -s /usr/share/apache-karaf-3.0.1/bin/karaf-service /etc/init.d/

Add the new service to the chkconfig management.

sudo chkconfig karaf-service --add

Tell chkconfig to start the karaf-service on boot time.

sudo chkconfig karaf-service on

If you start karaf with

sudo service karaf-service start

then you can read:

Startng karaf...

Now check whether the karaf is really running.

$ ps -ef | grep karaf
devteam   7685  4013  0 14:08 pts/0    00:00:00 grep karaf

On my machine the karaf isn’t running, the question is why? Fortunately there is a wrapper log file available with more information.

$ cat /usr/share/apache-karaf-3.0.1/data/log/wrapper.log
STATUS | wrapper  | 2014/09/04 14:02:50 | --> Wrapper Started as Daemon
STATUS | wrapper  | 2014/09/04 14:02:51 | Launching a JVM...
ERROR  | wrapper  | 2014/09/04 14:02:51 | Unable to start JVM: No such file or directory (2)
ERROR  | wrapper  | 2014/09/04 14:02:51 | JVM exited while loading the application.
STATUS | wrapper  | 2014/09/04 14:02:55 | Launching a JVM...
ERROR  | wrapper  | 2014/09/04 14:02:55 | Unable to start JVM: No such file or directory (2)
ERROR  | wrapper  | 2014/09/04 14:02:55 | JVM exited while loading the application.
STATUS | wrapper  | 2014/09/04 14:02:59 | Launching a JVM...
ERROR  | wrapper  | 2014/09/04 14:02:59 | Unable to start JVM: No such file or directory (2)
ERROR  | wrapper  | 2014/09/04 14:02:59 | JVM exited while loading the application.
STATUS | wrapper  | 2014/09/04 14:03:03 | Launching a JVM...
ERROR  | wrapper  | 2014/09/04 14:03:03 | Unable to start JVM: No such file or directory (2)
ERROR  | wrapper  | 2014/09/04 14:03:03 | JVM exited while loading the application.
STATUS | wrapper  | 2014/09/04 14:03:07 | Launching a JVM...
ERROR  | wrapper  | 2014/09/04 14:03:07 | Unable to start JVM: No such file or directory (2)
ERROR  | wrapper  | 2014/09/04 14:03:07 | JVM exited while loading the application.
FATAL  | wrapper  | 2014/09/04 14:03:07 | There were 5 failed launches in a row, each lasting less than 300 seconds.  Giving up.
FATAL  | wrapper  | 2014/09/04 14:03:07 |   There may be a configuration problem: please check the logs.
STATUS | wrapper  | 2014/09/04 14:03:07 | <-- Wrapper Stopped
 

So what happened? Karaf needs java to run and was trying to use the JVM. But the JVM can not be found! Above Karaf told us where we can find the wrapper configuration file.
Please open the file now.

sudo vi /usr/share/apache-karaf-3.0.1/etc/karaf-wrapper.conf

And search for the following lines.

#********************************************************************
# Wrapper Properties
#********************************************************************
set.default.JAVA_HOME=null

For some reasons JAVA_HOME is set to null here and further down in the configuration file you will find this line.

wrapper.java.command=%JAVA_HOME%/bin/java

Of course the command null/bin/java can not be found and the log file tells us:

Unable to start JVM: No such file or directory (2)

Change the line

set.default.JAVA_HOME=null

to something with more sense for example

set.default.JAVA_HOME=/usr/java/latest

or whereever your java has been installed.
Now try to start Karaf again.

sudo service karaf-service start

Let's check whether Karaf is really running or not.

$ ps -ef | grep karaf
root      7987     1  0 14:28 ?        00:00:00 /usr/share/apache-karaf-3.0.1/bin/karaf-wrapper /usr/share/apache-karaf-3.0.1/etc/karaf-wrapper.conf
root      7989  7987 18 14:28 ?        00:00:14 /usr/java/latest/bin/java -Dkaraf.home=/usr/share/apache-karaf-3.0.1 -Dkaraf.base=/usr/share/apache-
r:/usr/share/apache-karaf-3.0.1/lib/karaf.jar:/usr/share/apache-karaf-3.0.1/lib/karaf-jmx-boot.jar:/usr/share/apache-karaf-3.0.1/lib/karaf-jaas-boot
devteam   8045  4013  0 14:29 pts/0    00:00:00 grep karaf

Yes this time Karaf runs smoothly. But have you seen the ugly thing here? The Karaf process is running under user root. This is always a bad idea, never run a service as root!
Go and stop the karaf service.

sudo service karaf-service stop

In the next part we'll change the configuration to start Karaf as user karaf.

Start Apache Karaf 3.0.1 as user karaf and not as root

Add a new user group.

$ sudo groupadd karaf

Add a new user.

$ sudo useradd -s /bin/bash -g karaf karaf

Now change the owner of the apache-karaf-3.0.1 directory to karaf.

$ sudo chown -Rf karaf.karaf /usr/share/apache-karaf-3.0.1

Open the karaf service file and search for RUN_AS_USER.

sudo vi /etc/init.d/karaf-service

Change the line to.

RUN_AS_USER=karaf

and start karaf again.

sudo service karaf-service start

Check the Karaf process.

sudo ps -ef | grep karaf
karaf     8715     1  0 14:52 ?        00:00:00 /usr/share/apache-karaf-3.0.1/bin/karaf-wrapper /usr/share/apache-karaf-3.0.1/etc/karaf-wrapper.conf wrapper.syslog.ident=karaf wrapper.pidfile=/usr/share/apache-karaf-3.0.1/data/k
karaf     8717  8715 69 14:52 ?        00:00:03 /usr/java/latest/bin/java -Dkaraf.home=/usr/share/apache-karaf-3.0.1 -Dkaraf.base=/usr/share/apache-karaf-3.0.1 -Dkaraf.data=/usr/share/apache-karaf-3.0.1/data -Dkaraf.etc=/usr/sha
r:/usr/share/apache-karaf-3.0.1/lib/karaf.jar:/usr/share/apache-karaf-3.0.1/lib/karaf-jmx-boot.jar:/usr/share/apache-karaf-3.0.1/lib/karaf-jaas-boot.jar:/usr/share/apache-karaf-3.0.1/lib/karaf-wrapper-main.jar:/usr/share/apache-
devteam   8751  4013  0 14:52 pts/0    00:00:00 grep karaf

As you can see above, Apache Karaf runs under user karaf and as a daemon (service) now. After a system reboot karaf will be started automatically.
Thank you for reading my blog entry and please share if you like it.

Share on FacebookShare on LinkedInShare on Google+Tweet about this on TwitterEmail this to someone
PDF herunterladen
Posted in Infrastructure