Bitbucket don't start with service but will start with ./start-bitbucket.sh
Asked by Benoit FIEVEZ:

Hello,

I decided to install the last bitbucket version to my personal VM LAB running on virtualbox and a rockylinux 9.2

I installed without big issues bitbucket and can connect to the web interface on localhost:7990

Because I often turn off this lab and for best practice I decided to run bitbucket as a service as I already did for the Confluence and Jira already install on my VM.

So I created **/usr/lib/systemd/system/bitbucket.service**

Added this in my file (note that the path is a symbolic link)

```
[Unit]
Description=Atlassian Bitbucket Server Service
After=syslog.target network.target

[Service]
Type=forking
User=bitb
ExecStart=/opt/atlassian/bitbucket/bin/start-bitbucket.sh
ExecStop=/opt/atlassian/bitbucket/bin/stop-bitbucket.sh

[Install]
WantedBy=multi-user.target
```

did a **systemctl daemon-reload**after

**systemctl start bitbucket.service**gave me the result below

```

Job for bitbucket.service failed because the control process exited with error code.
See "systemctl status bitbucket.service" and "journalctl -xeu bitbucket.service" for details.
```

**systemctl status bitbucket.service**gave me this error

```

? bitbucket.service - Atlassian Bitbucket Server Service
Loaded: loaded (/usr/lib/systemd/system/bitbucket.service; enabled; preset: disabled)
Active: failed (Result: exit-code) since Wed 2023-08-30 11:53:53 CEST; 6s ago
Process: 5392 ExecStart=/opt/atlassian/bitbucket/bin/start-bitbucket.sh (code=exited, status=1/FAILURE)
CPU: 152ms

Aug 30 11:53:53 localhost.localdomain start-bitbucket.sh[5396]: --enable-preview
Aug 30 11:53:53 localhost.localdomain start-bitbucket.sh[5396]: allow classes to depend on preview features of this >
Aug 30 11:53:53 localhost.localdomain start-bitbucket.sh[5396]: To specify an argument for a long option, you can use --<name>=<value>>
Aug 30 11:53:53 localhost.localdomain start-bitbucket.sh[5396]: --<name> <value>.
Aug 30 11:53:53 localhost.localdomain start-bitbucket.sh[5392]: Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
Aug 30 11:53:53 localhost.localdomain start-bitbucket.sh[5392]: Edit set-jre-home.sh and define JRE_HOME
Aug 30 11:53:53 localhost.localdomain start-bitbucket.sh[5392]: Startup has been aborted
Aug 30 11:53:53 localhost.localdomain systemd[1]: bitbucket.service: Control process exited, code=exited, status=1/FAILURE
Aug 30 11:53:53 localhost.localdomain systemd[1]: bitbucket.service: Failed with result 'exit-code'.
Aug 30 11:53:53 localhost.localdomain systemd[1]: Failed to start Atlassian Bitbucket Server Service.
```

Despite having this line **JRE_HOME= /usr/lib/jvm/jre-11-openjdk/bin/java**

in the file **/opt/atlassian/bitbucket/bin/set-jre-home.sh**

I defined my JAVA_HOME variable in **/etc/profile** with thoses lines

```
export JAVA_HOME=/usr/lib/jvm/jre-11-openjdk
export PATH=$PATH:$JAVA_HOME/bin
```

**echo $JAVA_HOME**return this

```
/usr/lib/jvm/jre-11-openjdk
```

**java --version**return this

```

openjdk 11.0.19 2023-04-18 LTS
OpenJDK Runtime Environment (Red_Hat-11.0.19.0.7-2) (build 11.0.19+7-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-11.0.19.0.7-2) (build 11.0.19+7-LTS, mixed mode, sharing)
```

But when I switch manually to my bitucket user "bitb" and run the starting script **/opt/atlassian/bitbucket/bin/start-bitbucket.sh**bitbucket will start normally

Which I don't undertstand as my service start the same script with the same user

I tried to

* reboot the VM
* check access right to directories is correct where owner is bitbucket user
* comment and uncomment the JRE_HOME variable in the **set-jre-home.sh**
* Modify JAVA_HOME variable and set it to bitbucket user profile

Since the error I get is from thoses line of the **set-jre-home.sh** script there I guess there is something to do around there

```
# Uncomment and edit the line below to define JRE_HOME.
# If you use the Bitbucket installer, this value will be automatically set to point to the bundled JRE.
# Once a value is set here, existing JRE_HOME and JAVA_HOME values set in the environment will be overridden and ignored.
JRE_HOME= /usr/lib/jvm/jre-11-openjdk/bin/java

# Otherwise, use an existing installed JDK defined by JAVA_HOME
if [ -z "$JRE_HOME" ]; then
if [ -n "$JAVA_HOME" ] && [ -x "$JAVA_HOME/jre/bin/java" ]; then
# If JAVA_HOME points to a valid JDK, use its JRE
JRE_HOME="$JAVA_HOME/jre"
elif [ -n "$JAVA_HOME" ] && [ -x "$JAVA_HOME/bin/java" ]; then
# If JAVA_HOME appears to point to a JRE and not a JDK use this anyway
JRE_HOME="$JAVA_HOME"
fi
fi

# By this line, JRE_HOME should be defined
if [ -z "$JRE_HOME" ]; then
echo "Neither the JAVA_HOME nor the JRE_HOME environment variable is defined"
echo "Edit set-jre-home.sh and define JRE_HOME"
return 1
fi
```

In advance thanks for any help in this troubleshooting


Answers:
author: Charlie Misonne
body: I don't think you can have a space after the "=" in

```
JRE_HOME= /usr/lib/jvm/jre-11-openjdk/bin/java
```

Can you remove it and try again?

If it's still not working I will take a more in-depth look :-)

comments: author: Benoit FIEVEZ
body: Hello Charlie,  

Thanks for the answer.  

When I remove the space  

I got another error

```


Aug 31 10:24:05 localhost.localdomain start-bitbucket.sh[4444]: The JRE_HOME environment variable is not defined correctly
Aug 31 10:24:05 localhost.localdomain start-bitbucket.sh[4444]: This environment variable is needed to run this program
Aug 31 10:24:05 localhost.localdomain start-bitbucket.sh[4444]: Edit set-jre-home.sh and define JRE_HOME
Aug 31 10:24:05 localhost.localdomain start-bitbucket.sh[4444]: Startup has been aborted

```


author: Benoit FIEVEZ
body: In case someone find this again I found the solution to the problem  

Check where your java is installed here on Rocky Linux it will be set in /usr/lib/jvm

*** ** * ** ***

```
[root@localhost bin]# ll /usr/lib/jvm
total 0
drwxr-xr-x. 5 root root 68 Oct 19 14:26 java-11-openjdk-11.0.20.0.8-3.el9.x86_64
lrwxrwxrwx. 1 root root 21 Oct 19 14:28 jre -> /etc/alternatives/jre
lrwxrwxrwx. 1 root root 24 Oct 19 14:28 jre-11 -> /etc/alternatives/jre_11
lrwxrwxrwx. 1 root root 32 Oct 19 14:28 jre-11-openjdk -> /etc/alternatives/jre_11_openjdk
lrwxrwxrwx. 1 root root 40 Aug 2 05:50 jre-11-openjdk-11.0.20.0.8-3.el9.x86_64 -> java-11-openjdk-11.0.20.0.8-3.el9.x86_64
lrwxrwxrwx. 1 root root 29 Oct 19 14:28 jre-openjdk -> /etc/alternatives/jre_openjdk
```

The shortest symlink is jre so the directory path will be **/usr/lib/jvm/jre**

You need to set JRE HOME in /opt/atlassian/bitbucket/bin/**set-jre-home.sh**

```
# Uncomment and edit the line below to define JRE_HOME.
# If you use the Bitbucket installer, this value will be automatically set to point to the bundled JRE.
# Once a value is set here, existing JRE_HOME and JAVA_HOME values set in the environment will be overridden and ignored.
JRE_HOME=/usr/lib/jvm/jre
```

The mistake in previous post was **JRE_HOME= /usr/lib/jvm/jre-11-openjdk/bin/java** poiting to the application directory directly where the global directory is required.


Tags: bitbucket-server

server