Configure SonarQube to start as a service on Linux

Author
By Darío Rivera
Posted On in Static Code Analysis

After installing SonarQube and configuring a dedicated database, the last step for you is probably to configure it as a service so that it starts with the operating system. If you have not yet installed SonarQube, we recommend that you read the following posts.

- SonarQube Installation
- Configure PostgreSQL in SonarQube

Init.d

Once this is done, it's a matter of creating the /etc/init.d/sonarqube service configuration file and adding the following content.

#! /bin/sh
/usr/bin/sonarqube $*

After that, you must create a symbolic link to the system executables and give it the appropriate permissions.

sudo ln -s /var/opt/sonarqube-8/linux-x86-64/sonar.sh /usr/bin/sonarqube
sudo chmod 755 /etc/init.d/sonarqube
sudo update-rc.d Sonarqube defaults

After this, you can restart the server and check that it is running by checking the status of the service.

fermius@debian:~# service sonarqube status
● sonarqube.service
   Loaded: loaded (/etc/init.d/sonarqube; generated)
   Active: active (running) since Mon 2020-01-06 17:47:08 UTC; 13s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 644 ExecStart=/etc/init.d/sonarqube start (code=exited, status=0/SUCCESS)
    Tasks: 22 (limit: 2378)
   Memory: 188.6M
   CGroup: /system.slice/sonarqube.service
           ├─720 /var/opt/sonarqube-8/bin/linux-x86-64/./wrapper /var/opt/sonarqube-8/bin/linux-x86-64/../../conf/wrapper.conf wrapper.syslog.ident=SonarQube wrapper.pidfile=/var/opt/sonarqube-8/bin/linux
           └─722 java -Dsonar.wrapped=true -Djava.awt.headless=true -Xms8m -Xmx32m -Djava.library.path=./lib -classpath ../../lib/jsw/wrapper-3.2.3.jar:../../lib/common/sonar-webserver-8.1.0.31237.jar:../

Jan 06 17:47:07 debian-laravel systemd[1]: Starting sonarqube.service...
Jan 06 17:47:07 debian-laravel sonarqube[644]: Starting SonarQube...
Jan 06 17:47:08 debian-laravel sonarqube[644]: Started SonarQube.
Jan 06 17:47:08 debian-laravel systemd[1]: Started sonarqube.service.

SystemD

Another way to configure this service is with systemd. For this, you will need to create the /etc/systemd/system/sonarqube.service file and paste the following content.

[Unit]
Description=SonarQube service
After=syslog.target network.target

[Service]
Type=simple
User=sonarqube
Group=sonarqube
PermissionsStartOnly=true
ExecStart=/bin/nohup /usr/bin/java -Xms32m -Xmx32m -Djava.net.preferIPv4Stack=true -jar /var/opt/sonarqube-8/lib/sonar-application-8.1.0.31237.jar
StandardOutput=syslog
LimitNOFILE=65536
LimitNPROC=8192
TimeoutStartSec=5
Restart=always
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

You will need to change the respective variables if necessary, such as
- Java bin path (/usr/bin/java)
- SonarQube path (/var/opt/sonarqube-8)
- SonarQube user (sonarqube)
- SonarQube password (sonarqube)

Once this is done, all that remains is to configure the service.

sudo systemctl enable sonarqube.service
sudo systemctl start sonarqube.service

You can check the status of the service with the following command.

systemctl status sonarqube

If an error occurs while starting the service, you can check the log in the following way.

journalctl -u sonarqube.service | tail -n 30

This way, you can observe the last 30 lines of the log or as many as necessary.


Acerca de Darío Rivera

Author

Application Architect at Elentra Corp . Quality developer and passionate learner with 10+ years of experience in web technologies. Creator of EasyHttp , an standard way to consume HTTP Clients.

LinkedIn Twitter Instagram

Sólo aquellos que han alcanzado el éxito saben que siempre estuvo a un paso del momento en que pensaron renunciar.