# Stash Latest (2.9) : Running Stash as a Linux service

Added by pwatson , edited by ssaasen on Oct 09, 2013
Icon

System administration tasks are not supported by Atlassian. These instructions are only provided as a guide and may not be up to date with the latest version of your operating system.

For production use on a Linux server, Stash should be configured to run as a Linux service, that is, as a daemon process. This has the following advantages:

- Stash can be automatically restarted when the operating system restarts.
- Stash can be automatically restarted if it stops for some reason.
- Stash is less likely to be accidentally shut down, as can happen if the terminal Stash was manually started in is closed.
- Logs from the Stash JVM can be properly managed by the service.

Related pages:

- Installing Stash on Linux and Mac

There are different approaches to running Stash as a service on Linux:

- Use the Java Service Wrapper which a Java Application to be run as a UNIX Daemon.
- Use an `init.d` script to start Stash at boot time - this doesn't restart Stash if it stops for some reason.

Note that Stash assumes that the external database is available when it starts; these approaches do not support service dependencies, and the startup scripts will not wait for the external database to become available.

On this page:

## Using the Java Service Wrapper

Stash can be run as a service on Linux using the Java Service Wrapper. The Service Wrapper is known to work with Debian, Ubuntu, and Red Hat.

The Service Wrapper provides the following benefits:

- Allows Stash, which is a Java application, to be run as a service.
- No need for a user to be logged on to the system at all times, or for a command prompt to be open and running on the desktop to be able to run Stash.
- The ability to run Stash in the background as a service, for improved convenience, system performance and security.
- Stash is launched automatically on system startup and does not require that a user be logged in.
- Users are not able to stop, start, or otherwise tamper with Stash unless they are an administrator.
- Can provide advanced failover, error recovery, and analysis features to make sure that Stash has the maximum possible uptime.

Please see http://wrapper.tanukisoftware.com/doc/english/launch-nix.html for wrapper installation and configuration instructions.

The service wrapper supports the standard commands for SysV init scripts, so it should work if you just create a symlink to it from `/etc/init.d`.

## Using an init.d script

The usual way on Linux to ensure that a process restarts at system restart is to use an init.d script. This approach does not restart Stash if it stops by itself.

1. Stop Stash .

2. Create a stash user, set the permissions to that user, create a home directory for Stash and create a symlink to make upgrades easier:

       $> export VERSION="2.8.1" # Set this to the version you want to install
       $> curl -OL http://downloads.atlassian.com/software/stash/downloads/atlassian-stash-$VERSION.tar.gz
       $> tar xz -C /opt -f atlassian-stash-$VERSION.tar.gz
       $> ln -s /opt/atlassian-stash-$VERSION /opt/atlassian-stash-latest

       # Create a home directory
       $> mkdir /opt/stash-home

       # ! Update permissions and ownership accordingly

3. Create the startup script in `/etc/init.d/stash` with the following contents (Ensure the script is executable by running `chmod 755 stash`) Note: this is only an example, please adjust the script for your platform:

       #! /bin/sh
       ### BEGIN INIT INFO
       # Provides:          stash
       # Required-Start:    $remote_fs $syslog
       # Required-Stop:     $remote_fs $syslog
       # Default-Start:     2 3 4 5
       # Default-Stop:      0 1 6
       # Short-Description: Initscript for Atlassian Stash
       # Description:  Automatically start Atlassian Stash when the system starts up.
       #               Provide commands for manually starting and stopping Stash.
       ### END INIT INFO
       # Adapt the following lines to your configuration
       # RUNUSER: The user to run Stash as.
       RUNUSER=stash
       # STASH_INSTALLDIR: The path to the Stash installation directory
       STASH_INSTALLDIR="/opt/atlassian-stash"
       # STASH_HOME: Path to the Stash home directory
       STASH_HOME="/opt/stash-home"
       # ==================================================================================
       # ==================================================================================
       # ==================================================================================
       # PATH should only include /usr/* if it runs after the mountnfs.sh script
       PATH=/sbin:/usr/sbin:/bin:/usr/bin
       DESC="Atlassian Stash"
       NAME=stash
       PIDFILE=$STASH_INSTALLDIR/work/catalina.pid
       SCRIPTNAME=/etc/init.d/$NAME
       # Read configuration variable file if it is present
       [ -r /etc/default/$NAME ] && . /etc/default/$NAME
       # Define LSB log_* functions.
       # To be replaced by LSB functions
       # Defined here for distributions that don't define
       # log_daemon_msg
       log_daemon_msg () {
           echo $@
       }
       # To be replaced by LSB functions
       # Defined here for distributions that don't define
       # log_end_msg
       log_end_msg () {
           retval=$1
           if [ $retval -eq 0 ]; then
               echo "."
           else
               echo " failed!"
           fi
           return $retval
       }
       # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
       . /lib/lsb/init-functions

       run_with_home() {
           if [ "$RUNUSER" != "$USER" ]; then
               su - "$RUNUSER" -c "export STASH_HOME=${STASH_HOME};${STASH_INSTALLDIR}/bin/$1"
           else
               export STASH_HOME=${STASH_HOME};${STASH_INSTALLDIR}/bin/$1
           fi
       }
       #
       # Function that starts the daemon/service
       #
       do_start()
       {
           run_with_home start-stash.sh
       }
       #
       # Function that stops the daemon/service
       #
       do_stop()
       {
           if [ -e $PIDFILE ]; then
             run_with_home stop-stash.sh
           else
             log_failure_msg "$NAME is not running."
           fi
       }

       case "$1" in
         start)
           [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
           do_start
           case "$?" in
               0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
               2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
           esac
           ;;
         stop)
           [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
           do_stop
           case "$?" in
               0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
               2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
           esac
           ;;
         status)
              if [ ! -e $PIDFILE ]; then
                log_failure_msg "$NAME is not running."
                return 1
              fi
              status_of_proc -p $PIDFILE "" $NAME && exit 0 || exit $?
              ;;
         restart|force-reload)
           #
           # If the "reload" option is implemented then remove the
           # 'force-reload' alias
           #
           log_daemon_msg "Restarting $DESC" "$NAME"
           do_stop
           case "$?" in
             0|1)
               do_start
               case "$?" in
                   0) log_end_msg 0 ;;
                   1) log_end_msg 1 ;; # Old process is still running
                   *) log_end_msg 1 ;; # Failed to start
               esac
               ;;
             *)
               # Failed to stop
               log_end_msg 1
               ;;
           esac
           ;;
         *)
           echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
           exit 3
           ;;
       esac

4. To start on system boot, add the script to the start up process. For Ubuntu (and other Debian derivatives) use:

       update-rc.d stash defaults

   For RHEL (and derivates) use:

       chkconfig --add stash --level 0356

   Note: You may have to install the `redhat-lsb` package on RHEL (or derivatives) to provide the LSB functions used in the script.

   You may also be interested in an alternative startup script optimised for usage in RHEL based systems: https://answers.atlassian.com/questions/218946/stash-system-v-init-script-for-rhel

5. Verify that the Stash service comes back up after restarting the machine.