Chapter 4. Installing Apache Karaf as a Service

This chapter provides information on how you can start an Apache Karaf instance as a system service using the provided templates.

4.1. Overview

Using the service script templates, you can run a Karaf instance with the help of operating system specific init scripts. You can find these templates under the bin/contrib directory.

4.2. Running Karaf as a Service

The karaf-service.sh utility helps you to customize the templates. This utility automatically identifies the operating system and the default init system and generates ready-to-use init scripts. You can also customize the scripts to adapt them to the environment, by setting JAVA_HOME and a few other environment variables.

The generated scripts are composed of two files:

  • The init script
  • The init configuration file

4.3. Systemd

When the karaf-service.sh utility identifies systemd, it generates three files:

  • A systemd unit file to manage the root Apache Karaf container.
  • A systemd environment file with variables used by the root Apache Karaf container.
  • (Not supported) A systemd template unit file to manage Apache Karaf child containers.

For example, to set up a service for a Karaf instance installed at /opt/karaf-4, giving the service the name, karaf-4:

$ ./karaf-service.sh -k /opt/karaf-4 -n karaf-4
Writing service file "/opt/karaf-4/bin/contrib/karaf-4.service"
Writing service configuration file ""/opt/karaf-4/etc/karaf-4.conf"
Writing service file "/opt/karaf-4/bin/contrib/karaf-4@.service"
$ sudo cp /opt/karaf-4/bin/contrib/karaf-4.service /etc/systemd/system
$ sudo systemctl enable karaf-4.service

4.4. SysV

When the karaf-service.sh utility identifies a SysV system, it generates two files:

  • An init script to manage the root Apache Karaf container.
  • An environment file with variables used by the root Apache Karaf container.

For example, to set up a service for a Karaf instance installed at /opt/karaf-4, giving the service the name, karaf-4:

$ ./karaf-service.sh -k /opt/karaf-4 -n karaf-4
Writing service file "/opt/karaf-4/bin/contrib/karaf-4"
Writing service configuration file "/opt/karaf-4/etc/karaf-4.conf"
$ sudo ln -s /opt/karaf-4/bin/contrib/karaf-4 /etc/init.d/
$ sudo chkconfig karaf-4 on
Note

To enable the service startup upon boot, refer to your operating system init guide.

4.5. Solaris SMF

When the karaf-service.sh utility identifies a Solaris operating system, it generates a single file.

For example, to set up a service for a Karaf instance installed at /opt/karaf-4, giving the service the name, karaf-4:

$ ./karaf-service.sh -k /opt/karaf-4 -n karaf-4
Writing service file "/opt/karaf-4/bin/contrib/karaf-4.xml"
$ sudo svccfg validate /opt/karaf-4/bin/contrib/karaf-4.xml
$ sudo svccfg import /opt/karaf-4/bin/contrib/karaf-4.xml
Note

The generated SMF descriptor is defined as transient, so that you can execute the start method only once.

4.6. Windows

Installation of Apache Karaf as Windows service is supported through winsw.

To install Apache Karaf as Windows service, perform the following steps:

  1. Rename the karaf-service-win.exe file to karaf-4.exe.
  2. Rename the karaf-service-win.xml file to karaf-4.xml.
  3. Customize the service descriptor as required.
  4. Use the service executable to install, start and stop the service.

For example:

C:\opt\apache-karaf-4\bin\contrib> karaf-4.exe install
C:\opt\apache-karaf-4\bin\contrib> karaf-4.exe start

4.7. karaf-service.sh Options

You can specify options to the karaf-service.sh utility either as command-line options or by setting environment variables, as follows:

Command Line OptionEnvironment VariableDescription

-k

KARAF_SERVICE_PATH

Karaf installation path

-d

KARAF_SERVICE_DATA

Karaf data path (defaults to ${KARAF_SERVICE_PATH}/data)

-c

KARAF_SERVICE_CONF

Karaf configuration file (defaults to ${KARAF_SERVICE_PATH}/etc/${KARAF_SERVICE_NAME}.conf)

-t

KARAF_SERVICE_ETC

Karaf etc path (defaults to ${KARAF_SERVICE_PATH}/etc})

-p

KARAF_SERVICE_PIDFILE

Karaf PID path (defaults to ${KARAF_SERVICE_DATA}/${KARAF_SERVICE_NAME}.pid)

-n

KARAF_SERVICE_NAME

Karaf service name (defaults to karaf)

-e

KARAF_ENV

Specifies an environment variable setting, NAME=VALUE, for the service (can be specified more than once)

-u

KARAF_SERVICE_USER

Karaf user

-g

KARAF_SERVICE_GROUP

Karaf group (defaults to ${KARAF_SERVICE_USER})

-l

KARAF_SERVICE_LOG

Karaf console log (defaults to ${KARAF_SERVICE_DATA}/log/${KARAF_SERVICE_NAME}-console.log)

-f

KARAF_SERVICE_TEMPLATE

Template file to use

-x

KARAF_SERVICE_EXECUTABLE

Karaf executable name (defaults to karaf — must support the daemon and stop commands)

-h

 

Help message