Skip to main content

Run IQ Server as a Service

The server can be run as a service or daemon. This is recommended to ensure the server will restart when the operating system reboots. The following instructions are for Linux environments.

See the KB to run as a service on a Windows Server

  1. Set the user: iqserver on the server with limited access

  2. Add the service script to /etc/init.d/sonatype-iq-server

  3. Set the systemV or systemd depending on your configuration

Set directory permissions

Create a dedicated user with limited access for the service.

The user needs full access rights to the server installation directory and the sonatype-work directory listed in the config YAML.

The following command grants service user permissions.

chown -Rv {username} /opt/sonatype-iq-server

Setting the service script will vary between operating systems and distributions depending on the init system used. The script would be copied to a dedicated startup directory and assigned run-levels and other characteristics for the start-up.

Save the following script as sonatype-iq-server in the /etc/init.d/ directory.

/etc/init.d/sonatype-iq-server
#!/bin/sh

# The following comment lines are used by the init setup script like the
# chkconfig command for RedHat-based distributions. Change as
# appropriate for your installation.

### BEGIN INIT INFO
# Provides:          sonatype-iq-server
# Required-Start:    $local_fs $remote_fs $network $time $named
# Required-Stop:     $local_fs $remote_fs $network $time $named
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: sonatype-iq-server service
# Description:       Start the sonatype-iq-server service
### END INIT INFO

IQ_SERVER_HOME="/opt/sonatype-iq-server"
VERSION="[0-9.]*-??"
JAVA_OPTIONS="-Xmx4096m -XX:+UseG1GC"

# The user requires privileges to write into the sonatype-work directory.
RUN_AS_USER="iqserver"

do_start()
{
    _cmd &
    echo "Started sonatype-iq-server"
}

do_console()
{
    _cmd
}

do_stop()
{
    pid=`ps aux | grep nexus-iq-server | grep '.jar server' | grep -vE '(stop|grep)' | awk '{print $2}'`
    kill $pid
    echo "Stopping sonatype-iq-server - PID $pid ($?)"
}

do_usage()
{
    echo "Usage: sonatype-iq-server [console|start|stop]"
}

_cmd()
{
    cd $IQ_SERVER_HOME || return $?
    chown $RUN_AS_USER ./stderr.log
    if [ "$RUN_AS_USER" == "$USER" ]; then
        java $JAVA_OPTIONS -jar nexus-iq-server-$VERSION.jar server config.yml 2> stderr.log
    else
        sudo -u $RUN_AS_USER java $JAVA_OPTIONS -jar nexus-iq-server-$VERSION.jar server config.yml 2> stderr.log
    fi 
}

case $1 in
    console)
        do_console
        ;;
    start)
        do_start
        ;;
    stop)
        do_stop
        ;;
    *)
        do_usage
        ;;
esac

SystemV

The following commands show an example of a Debian-based system. Depending on the requirements of your system administrator, modify the scripts to fit into your environment and the exact deployment scenario.

sudo su
cd /etc/init.d
update-rc.d sonatype-iq-server defaults
service sonatype-iq-server start

SystemD

This example is a script that uses systemd to run the server.

  1. Make sure the iqserver user exists

  2. Create the sonatype-iq-server script in /etc/init.d

  3. Create a file called sonatypeiq.service in the /etc/systemd/system/ directory

    /etc/systemd/system/sonatypeiq.service
  4. Add the following script and save the file

[Unit]
Description=Sonatype IQ Service
After=network-online.target

[Service]
Type=forking
ExecStart=/etc/init.d/sonatype-iq-server start
ExecStop=/etc/init.d/sonatype-iq-server stop
User=iqserver
Restart=on-abort
TimeoutSec=600

[Install]
WantedBy=multi-user.target

Activate the service with the following commands

chmod a+x /etc/init.d/sonatype-iq-server
sudo systemctl daemon-reload
sudo systemctl enable sonatypeiq.service
sudo systemctl start sonatypeiq.service

Native Linux installers from the community

Community-provided native installers for Linux are available to set up the server to run as a service, however, they are not officially supported.

See github.com/sonatype-nexus-community/nexus-iq-server-installer