Run as a Service In Linux - Documentation topics on: bsd,centos,linux,service,services,systemd,sysv,.

Run as a Service In Linux

Running dotCMS as a service in Linux can be done in a number of different ways, depending on your Linux distribution and personal taste. Normally, it's best to use the method that's consistent with the installed system.

The following examples assume that dotCMS is installed in /srv/dotcms and Java is installed in /usr/local/java. Substitute appropriate paths to match your environment.

BSD Style

Using a BSD style script is the easiest way to make dotCMS start every time the Linux server starts. To do this, just add the necessary lines to the /etc/rc.local file. For example:

export JAVA_HOME=/usr/local/java
/srv/dotcms/bin/startup.sh

Sys V Style

Sys V init scripts are more complex, but provide more control over how to start and stop dotCMS. To use the following init script, add the commands to a script in the /etc/init.d directory, and create the appropriate symbolic links for the desired run levels (/etc/rc3.d, /etc/rc5.d, etc). Be sure to change the JAVA_HOME and DOTCMS_HOME environment variables to match your environment.

#! /bin/sh
### BEGIN INIT INFO
# Provides:          dotcms
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      S 0 1 6
# Short-Description: dotCMS initscript
# Description:       dotCMS init script
### END INIT INFO

JAVA_HOME=/usr/local/java
DOTCMS_HOME=/srv/dotcms

PATH=/usr/sbin:/usr/bin:/sbin:/bin
DESC="dotCMS"
NAME=catalina.sh
DAEMON=$DOTCMS_HOME/bin/$NAME
DAEMON_ARGS="start"
DOTSERVER=`echo $DOTCMS_HOME | sed -e 's/\(.*\)\///'`
PIDFILE=/var/run/$DOTSERVER.pid
SCRIPTNAME=/etc/init.d/dotcms

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Load the VERBOSE setting and other rcS variables
[ -f /etc/default/rcS ] && . /etc/default/rcS

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
. /lib/lsb/init-functions

#
# Function that starts the daemon/service
#
do_start()
{
    $DAEMON start
    return 1
}

#
# Function that stops the daemon/service
#
do_stop()
{
    # Return
    #   0 if daemon has been stopped
    #   1 if daemon was already stopped
    #   2 if daemon could not be stopped
    #   other if a failure occurred
    if [ -e $PIDFILE ]
    then
        $DAEMON stop 
        if [ -e $PIDFILE ]
        then
            # wasn't stopped
            return 2
        fi
        return 0
    fi
    return 1
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
    #
    # If the daemon can reload its configuration without
    # restarting (for example, when it is sent a SIGHUP),
    # then implement that here.
    #
    do_stop
    do_start
    return 0
}

case "$1" in
  start)
    do_start
    ;;
  stop)
    do_stop
    ;;
  restart|force-reload)
    do_reload
    ;;
  *)
    #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
    echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
    exit 3
    ;;
esac

:

Systemd Style

Some other OS’s like CentOS 7 are shipped with systemd, which make it more straightforward to configure dotCMS as a service compared to SysV.

To use this method, you need to perform the following steps:

  1. Create the Environment File
  2. Create a temporary directory to store the PID File
  3. Create the Unit File
  4. Run systemctl to control the dotCMS service

1. Create the Environment File

Create a file named /etc/sysconfig/dotcms (no extension) with the following contents:

JAVA_HOME=/opt/oracle/java/latest/
PIDFile=/var/run/dotcms/dotcms.pid
WorkingDirectory=/opt/dotcms/wwwroot/current

Make sure that:

  • The JAVA_HOME environment variable is set to the path of the JDK you actually want to use.
  • The PIDFile environment variable is consistent with how dotCMS is configured.
  • The WorkingDirectory environment variable is set to the current dotCMS installation path.

This file sets the environment variables needed in the script, and is used by the Unit File (below).

2. Create a temporary directory to store dotCMS's' PID file

Make use of tmpfiles.d to create a file named /etc/tmpfiles.d/dotcms.conf with the following contents:

D /var/run/dotcms 0755 dotcms dotcms

The system will use this file to create a temporary directory to store the dotCMS service PID number at startup.

3. Create a unit file for dotCMS

Create the file /usr/lib/systemd/system/dotcms.service, with the following contents:

# Unit file for dotCMS
[Unit]
Description=dotCMS Service
After=network.target

[Service]
Type=forking
EnvironmentFile=/etc/sysconfig/dotcms
User=dotcms
Group=dotcms
KillMode=none
ExecStart=/bin/sh ${WorkingDirectory}/bin/startup.sh
ExecStop=/bin/sh ${WorkingDirectory}/bin/shutdown.sh

[Install]
WantedBy=multi-user.target

Again, verify that all variables are set properly according to your deployment needs, and make sure of the following:

  • The user and group must be defined in the unit file.
  • The dotcms user must be created before starting dotCMS.

This file specifies the settings, files and commands necessary to run dotCMS as a service. This file name (but not the full path) is referenced when running the systemctl command (see below).

4. Control the dotCMS service using the systemctl command

Run the systemctl command to enable, disable, start or stop the dotCMS service, or to check the status of the service.

systemctl [enable|disable|start|stop|status] dotcms.service