Run Bitbucket as a Linux service

This page describes how to run Bitbucket Data Center and Server as a Linux service, and only applies if you are manually installing or upgrading Bitbucket from an archive file. See the page Install Bitbucket Server from an archive file for more details. 

Bitbucket 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.

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

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

アトラシアンではシステム管理タスクをサポートしていません。これらの手順はガイドとしてのみ提供されており、ご使用のオペレーティング システムの最新版に対応していない可能性があります。


Java Service Wrapper の使用

Bitbucket 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.

Service Wrapper には以下のメリットがあります。

  • Allows Bitbucket, 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 Bitbucket.
  • The ability to run Bitbucket in the background as a service, for improved convenience, system performance and security.
  • Bitbucket 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 Bitbucket unless they are an administrator.
  • Can provide advanced failover, error recovery, and analysis features to make sure that Bitbucket has the maximum possible uptime.

Wrapper のインストールと構成の手順については、http://wrapper.tanukisoftware.com/doc/english/launch-nix.html を参照してください。

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.

init.d スクリプトの使用

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 Bitbucket if it stops by itself.

  1. Start and stop Bitbucket.
  2. Create a user, set the permissions to that user, create a home directory for Bitbucket and create a symlink to make upgrades easier:

    $> curl -OL https://www.atlassian.com/software/stash/downloads/binary/atlassian-bitbucket-X.Y.Z.tar.gz
    $> tar xz -C /opt -f atlassian-bitbucket-X.Y.Z.tar.gz
    $> ln -s /opt/atlassian-bitbucket-X.Y.Z /opt/atlassian-bitbucket-latest
     
    # Create a home directory
    $> mkdir /opt/bitbucket-home
     
    # ! Update permissions and ownership accordingly

    (Be sure to replace X.Y.Z in the above commands with the version number of Bitbucket.)

  3. Create the startup script in /etc/init.d/bitbucket with the following contents (Ensure the script is executable by running chmod 755 bitbucket):

    #! /bin/sh
    
    ### BEGIN INIT INFO
    # Provides:          bitbucket
    # 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 Bitbucket Server
    # Description:  Automatically start Atlassian Bitbucket Server when the system starts up.
    #               Provide commands for manually starting and stopping Bitbucket Server.
    ### END INIT INFO
    
    # Adapt the following lines to your configuration
    # RUNUSER: The user to run Bitbucket Server as.
    RUNUSER=vagrant
    
    # BITBUCKET_INSTALLDIR: The path to the Bitbucket Server installation directory
    BITBUCKET_INSTALLDIR="/opt/atlassian-bitbucket-X.Y.Z"
    
    # BITBUCKET_HOME: Path to the Bitbucket home directory
    BITBUCKET_HOME="/opt/bitbucket-home"
    
    # ==================================================================================
    # ==================================================================================
    # ==================================================================================
    
    # PATH should only include /usr/* if it runs after the mountnfs.sh script
    PATH=/sbin:/usr/sbin:/bin:/usr/bin
    DESC="Atlassian Bitbucket Server"
    NAME=bitbucket
    PIDFILE=$BITBUCKET_HOME/log/bitbucket.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.
    # 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 BITBUCKET_HOME=${BITBUCKET_HOME};${BITBUCKET_INSTALLDIR}/bin/$1"
        else
            export BITBUCKET_HOME=${BITBUCKET_HOME};${BITBUCKET_INSTALLDIR}/bin/$1
        fi
    }
    
    #
    # Function that starts the daemon/service
    #
    do_start()
    {
        run_with_home start-bitbucket.sh
    }
    
    #
    # Function that stops the daemon/service
    #
    do_stop()
    {
        if [ -e $PIDFILE ]; then
          run_with_home stop-bitbucket.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

システム ブートで実行する

  1. システム ブートで開始するには、スタートアップ プロセスにスクリプトを追加します。
    Ubuntu (およびその他の Debian 派生製品) の場合:

    update-rc.d bitbucket defaults


    RHEL (およびその他の派生製品) の場合:

    chkconfig --add bitbucket --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.

  2. Verify that the Bitbucket service comes back up after restarting the machine.


systemd unit ファイルの使用

Thanks to Patrick Nelson for calling out this approach, which he set up for a Fedora system. It also works on other distributions that use systemd as the init system. This approach does not restart Bitbucket if it stops by itself.

  1. Create a bitbucket.service file in your /etc/systemd/system/ directory with the following lines:

    [Unit]
    Description=Atlassian Bitbucket Server Service
    After=syslog.target network.target
     
    [Service]
    Type=forking
    User=atlbitbucket
    ExecStart=/opt/atlassian-bitbucket-X.Y.Z/bin/start-bitbucket.sh
    ExecStop=/opt/atlassian-bitbucket-X.Y.Z/bin/stop-bitbucket.sh
     
    [Install]
    WantedBy=multi-user.target


    The value for User should be adjusted to match the user that Bitbucket runs as. ExecStart and ExecStop should be adjusted to match the path to your <Bitbucket installation directory>.

  2. ターミナルで次のコマンドを実行し、起動時のサービス開始を有効にします。

    systemctl enable bitbucket.service
  3. Start and stop Bitbucket, then restart the system, to check that Bitbucket starts as expected.
  4. 次のコマンドを使用してサービスを管理します。
    サービスの無効化
    systemctl disable bitbucket.service
    起動時にサービスが開始するように設定されていることの確認
    if [ -f /etc/systemd/system/*.wants/bitbucket.service ]; then echo "On"; else echo "Off"; fi
    サービスを手動で開始および停止 
    systemctl start bitbucket
    systemctl stop bitbucket
    Bitbucket Server のステータスの確認
    systemctl status bitbucket
Last modified on Mar 10, 2021

この内容はお役に立ちましたか?

はい
いいえ
この記事についてのフィードバックを送信する
Powered by Confluence and Scroll Viewport.