Metabase: How to Run Metabase jar as a service on Debian

Created on 21 Aug 2017  路  3Comments  路  Source: metabase/metabase

Disclaimer: This is a solution, not an issue. I wasn't sure where to submit this, and didn't see it documented anywhere or discussed in an issue. I'm assuming others may find this useful, but feel free to close if it isn't.

For those people who don't use docker in their infrastructure, there's still a need to setup Metabase in production. On Debian-based systems, that means registering Metabase as a service that can be started/stopped/uninstalled. This is just a recipe for a bare-bones approach, and anyone can take it from here to do what they need to do on their systems.

1. Create service script at /etc/init.d/metabase

$ sudo touch /etc/init.d/metabase
$ sudo <your-editor> /etc/init.d/metabase

In /etc/init.d/metabase, replace the items in <> with whatever is sensible for your system:

#!/bin/sh
### BEGIN INIT INFO
# Provides:          Metabase
# Required-Start:    $local_fs $network $named $time $syslog
# Required-Stop:     $local_fs $network $named $time $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Description:       Metabase stats/analytics/intelligence platform
### END INIT INFO

METABASE=</your/path/to/>metabase.jar
METABASE_CONFIG=/etc/default/metabase
RUNAS=<your_deploy_user>
PIDFILE=/var/run/metabase.pid
LOGFILE=/var/log/metabase.log

start() {
  if [ -f "$PIDFILE" ] && kill -0 $(cat "$PIDFILE"); then
    echo 'Metabase already running' >&2
    return 1
  fi
  echo 'Starting Metabase...' >&2
  local CMD="nohup java -jar \"$METABASE\" &> \"$LOGFILE\" & echo \$!"
  # load metabase env vars
  . "$METABASE_CONFIG"
  su -c "$CMD" $RUNAS > "$PIDFILE"
  echo 'Metabase started.' >&2
}

stop() {
  if [ ! -f "$PIDFILE" ] || ! kill -0 $(cat "$PIDFILE"); then
    echo 'Metabase not running' >&2
    return 1
  fi
  echo 'Stopping Metabase ...' >&2
  kill -15 $(cat "$PIDFILE") && rm -f "$PIDFILE"
  echo 'Metabase stopped.' >&2
}

uninstall() {
  echo -n "Are you really sure you want to uninstall Metabase? That cannot be undone. [yes|No] "
  local SURE
  read SURE
  if [ "$SURE" = "yes" ]; then
    stop
    rm -f "$PIDFILE"
    rm -f "$METABASE_CONFIG"
    # keep logfile around
    echo "Notice: log file is not be removed: '$LOGFILE'" >&2
    update-rc.d -f metabase remove
    rm -fv "$0"
  fi
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  uninstall)
    uninstall
    ;;
  retart)
    stop
    start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|uninstall}"
esac

2. Create Metabase env vars in /etc/default/metabase

$ sudo touch /etc/default/metabase
$ sudo <your-editor> /etc/default/metabase

In /etc/default/metabase, replace the items in <> with whatever is sensible for your system:

#!/bin/sh
export MB_PASSWORD_COMPLEXITY=<weak|normal|strong>
export MB_PASSWORD_LENGTH=<10>
export MB_JETTY_HOST=<0.0.0.0>
export MB_JETTY_PORT=<12345>
export MB_DB_TYPE=<postgres>
export MB_DB_DBNAME=<your_metabase_db>
export MB_DB_PORT=<5432>
export MB_DB_USER=<your_metabase_db_user>
export MB_DB_PASS=<ssshhhh>
export MB_DB_HOST=<localhost>
export MB_EMOJI_IN_LOGS=<'true'|'false'>
# whatever other env vars you want available

3. Register service

Now, some final prep & then you're done!

$ sudo chmod +x /etc/init.d/metabase
$ sudo touch /var/log/metabase.log
$ sudo chown <your_deploy_user>:<your_deploy_user> /var/log/metabase.log
$ sudo update-rc.d metabase defaults
$ sudo service metabase start

Most helpful comment

@bobwaycott I think a good place for this kind of info is the Operations Guide http://www.metabase.com/docs/latest/operations-guide/start.html. I don't know if many people will find it here. I suggest you should try to understand how to contribute to this file https://github.com/metabase/metabase/blob/master/docs/operations-guide/start.md

All 3 comments

@bobwaycott I think a good place for this kind of info is the Operations Guide http://www.metabase.com/docs/latest/operations-guide/start.html. I don't know if many people will find it here. I suggest you should try to understand how to contribute to this file https://github.com/metabase/metabase/blob/master/docs/operations-guide/start.md

Gotcha. I'll take a stab at it, @fernandosjp

Submitted PR #5798. Going to close this since it isn't a big/issue & I don't want to dirty the issue list up.

Was this page helpful?
0 / 5 - 0 ratings