Starting the RM Agent as a service on Linux

To ensure that the RM Agent starts by itself when the host machine is restarted, you can set up the environment in such a way that the RM Agent can be started as a service.

Before you begin

Previously to starting a Resource Monitoring Agent as a service on Windows, you need to have:
  • Java 8 installed on the host machine.
  • HCL OneTest™ Server added to the PATH environment variable.
  • an offline token to connect the agent securely with appropriate permissions. You can create an offline token from the User menu of the Resource Monitoring dashboard or you can re-use your active offline token. The token expires if it is not used for a month.

About this task

This topic relies on systemd services that is the default on most modern Linux distributions. Other ways may require adaptations of the instructions but the provided script will be a good basis in most cases.

Procedure

  1. Create a folder on your local hard drive like /opt/RMAgent-linuxservice.
  2. Download the agent jar file from the Resource Monitoring web page and save it to the same directory. For information about downloading the agent, see Installing the RM Agents.
  3. Create a new file /etc/systemd/system/RMAgent-linuxservice.service.
  4. Add the following content to this new file:
    [Unit]
    Description = Resource Monitoring Agent
    After = network.target
    
    [Service]
    Type = forking
    ExecStart = /opt/RMAgent-linuxservice/RMAgent-linuxservice.sh start
    ExecStop = /opt/RMAgent-linuxservice/RMAgent-linuxservice.sh stop
    Restart = on-failure
    RestartSec = 10
    
    [Install]
    WantedBy = multi-user.target
  5. Create another file /opt/RMAgent-linuxservice/RMAgent-linuxservice.sh.
    #!/bin/sh
    
    # Update with the Quality Server's host name
    sudo HCL_ONETEST_OFFLINE_TOKEN=(Enter your offline token here) java -jar (Enter the name of the downloaded jar file here)
    --serviceUrl=https://<service-hostname>/rm --projectId=<projet-id>
    
    ARGS="--serviceUrl=$SERVICE_URL --trustServiceCertificate --autoUpgradeDownloadThen=exitFailure"
    
    SCRIPT=$(readlink -f "$0")
    RMAGENT_HOME=$(dirname "$SCRIPT")
    
    # Ensure we're using the latest downloaded jar file
    PATH_TO_JAR=`ls -t $RMAGENT_HOME/com.hcl.test.rm.agent-*.jar | head -1`
    if [ -z "$PATH_TO_JAR" ]
    then
      cd $RMAGENT_HOME && { curl -k -O -J $SERVICE_URL/agent-jar; cd -; }
      PATH_TO_JAR=`ls -t $RMAGENT_HOME/com.hcl.test.rm.agent-*.jar | head -1`
      if [ -z "$PATH_TO_JAR" ]
      then
        echo "Start the server at $SERVICE_URL to allow download of the latest agent jar file"
        echo "Exiting..."
        exit 1
      fi
    fi
    SERVICE_NAME="Resource Monitoring Agent"
    #Pid file will reside in this script’s folder
    PATH_TO_PID=$RMAGENT_HOME/RMAgent-pid
    #Log file will reside in this script’s folder
    PATH_TO_LOG=$RMAGENT_HOME/RMAgent.log
    
    case $1 in
        start)
            echo "Starting $SERVICE_NAME ..."
            if [ ! -f $PATH_TO_PID ]; then
                nohup java -jar $PATH_TO_JAR $ARGS >> $PATH_TO_LOG 2>&1 &
                            echo $! > $PATH_TO_PID
                echo "$SERVICE_NAME started ..."
            else
                echo "$SERVICE_NAME is already running ..."
            fi
        ;;
        stop)
            if [ -f $PATH_TO_PID ]; then
                PID=$(cat $PATH_TO_PID);
                echo "$SERVICE_NAME stopping ..."
                kill $PID;
                echo "$SERVICE_NAME stopped ..."
                rm $PATH_TO_PID
            else
                echo "$SERVICE_NAME is not running ..."
            fi
        ;;
        restart)
            if [ -f $PATH_TO_PID ]; then
                PID=$(cat $PATH_TO_PID);
                echo "$SERVICE_NAME stopping ...";
                kill $PID;
                echo "$SERVICE_NAME stopped ...";
                rm $PATH_TO_PID
                echo "$SERVICE_NAME starting ..."
                nohup java -jar $PATH_TO_JAR $ARGS >> $PATH_TO_LOG 2>&1 &
                            echo $! > $PATH_TO_PID
                echo "$SERVICE_NAME started ..."
            else
                echo "$SERVICE_NAME is not running ..."
            fi
        ;;
    esac
    Note:
    • Replace <service-hostname> by the host name of the machine that runs HCL Quality Server.
    • Replace <project-id> by the project which is the number you find in the service URL.

Results

The Resource Monitoring agent will start automatically when the host machine restarts.
Feedback