<commit_before>#!/bin/bash
#
#   /etc/rc.d/init.d/flume_<%= @attributes["instanceName"] %>.sh
#
#   Management script for a Flume agent by the name of <%= @attributes["instanceName"] %>.

# NOTE: The following two lines are used for (un)installation via the "chkconfig" utility available on some systems, most notably Red Hat.
# chkconfig: 2345 20 80
# description: Management script for a Flume agent by the name of <%= @attributes["instanceName"] %>.

if [ -z "$JAVA_HOME" ]; then
    export JAVA_HOME="/usr/java/default/"
fi

FLUME_HOME=<%= @attributes["installDir"] %>
FLUME_BIN=bin/flume-ng
FLUME_AGENT_NAME=<%= @attributes["instanceName"] %>
SERVICE_NAME="<%= @attributes["serviceName"] %>"
PID_FILE=/var/run/$SERVICE_NAME
LOCK_FILE=/var/lock/subsys/$SERVICE_NAME
AGENT_USER=<%= @attributes["userName"] %>
PGREP_COMMAND="pgrep -f $FLUME_HOME/conf"

start() {
    echo -n "Starting $SERVICE_NAME: "
    agentRunning
    RUNNING=$?
    if [ $RUNNING -eq 0 ]; then
        echo "The Flume agent $SERVICE_NAME is already running; stop it before attempting to start a new agent process."
        return 1
    fi

    su -l $AGENT_USER -c "cd $FLUME_HOME;$FLUME_BIN agent -n $FLUME_AGENT_NAME -c conf -f <%= @attributes["outputConfigurationFile"] %> &"
    PIDS=`$PGREP_COMMAND`
    if [ -z "$PIDS" ]; then
        echo "Failed to start Flume agent. Examine the logs at $FLUME_HOME/logs/flume.log for more information."
        return 1
    else
        # Make sure the agent is actually running
        echo "started ($PIDS)"
        echo $PIDS > $PID_FILE
        touch $LOCK_FILE
        agentRunning
        RUNNING=$?
        if [ $RUNNING -gt 0 ]; then
            echo "Although the process was successfully invoked, the agent failed to start properly (status code $RUNNING). Examine the logs at $FLUME_HOME/logs/flume.log for more information."
            return 1
        fi
        return 0
    fi
}

stop() {
    echo "Stopping $SERVICE_NAME: "
    RETVAL=0
    agentRunning
    RUNNING=$?
    if [ $RUNNING -eq 0 ]; then
        PID=`head -1 $PID_FILE`
        kill $PID
        RETVAL=$?
        # Wait for either the process to end or for it to be a zombie process
        while [[ $RUNNING -eq 0 ]]; do
            sleep 1
            agentRunning
            RUNNING=$?
        done
        echo "Stopped PID $PID"
    else
        echo "There is no known service to stop."
    fi
    rm -f $LOCK_FILE
    echo "Service $SERVICE_NAME stopped."
    return $RETVAL
}

status() {
    agentRunning
    RUNNING=$?
    if [ $RUNNING -eq 0 ]; then
        echo "Agent is running"
    else
        echo "Agent is not running or cannot be determined to be running (status code: $RUNNING)"
    fi

    return $RUNNING
}

# Check to see if the agent is running
#
# Returns (per http://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html):
# - 0: the service is running and OK
# - 1: the service is not running, but the /var/run PID file exists
# - 3: the service is not running
# - 4: the status of the service cannot be determined
agentRunning() {
    if [ ! -e $PID_FILE ]; then
        echo "The PID file $PID_FILE was not found; the agent is assumed to not be running"
        return 3
    fi
    PID=`head -1 $PID_FILE`
    if [[ -d /proc/$PID ]]; then
        if [[ -n `grep zombie /proc/$PID/status` ]]; then
            echo "Warning! A zombie process was detected for $PID"
        fi
        return 0
    else
        # It's possible that the Flume agent was started outside of this script, which means that the agent cannot be started
        pids=()
        for pid in `$PGREP_COMMAND`
        do
            pids+=("$pid")
        done
        if [ ${#pids[@]} -gt 0 ]; then
            echo "An instance of the Flume agent (see: $PID_FILE) that is not being tracked by this script was found."
            return 4
        fi

        # The /var/run file exists, but the process isn't actually running
        return 3
    fi
}

case "$1" in
    start)
        start
        ;;
    stop)<commit_msg>Fixes #9: tolerate multiple PIDs<commit_after>#!/bin/bash
#
#   /etc/rc.d/init.d/flume_<%= @attributes["instanceName"] %>.sh
#
#   Management script for a Flume agent by the name of <%= @attributes["instanceName"] %>.

# NOTE: The following two lines are used for (un)installation via the "chkconfig" utility available on some systems, most notably Red Hat.
# chkconfig: 2345 20 80
# description: Management script for a Flume agent by the name of <%= @attributes["instanceName"] %>.

if [ -z "$JAVA_HOME" ]; then
    export JAVA_HOME="/usr/java/default/"
fi

FLUME_HOME=<%= @attributes["installDir"] %>
FLUME_BIN=bin/flume-ng
FLUME_AGENT_NAME=<%= @attributes["instanceName"] %>
SERVICE_NAME="<%= @attributes["serviceName"] %>"
PID_FILE=/var/run/$SERVICE_NAME
LOCK_FILE=/var/lock/subsys/$SERVICE_NAME
AGENT_USER=<%= @attributes["userName"] %>
PGREP_COMMAND="pgrep -f $FLUME_HOME/conf"

start() {
    echo -n "Starting $SERVICE_NAME: "
    agentRunning
    RUNNING=$?
    if [ $RUNNING -eq 0 ]; then
        echo "The Flume agent $SERVICE_NAME is already running; stop it before attempting to start a new agent process."
        return 1
    fi

    su -l $AGENT_USER -c "cd $FLUME_HOME;$FLUME_BIN agent -n $FLUME_AGENT_NAME -c conf -f <%= @attributes["outputConfigurationFile"] %> &"
    PIDS=`$PGREP_COMMAND`
    if [ -z "$PIDS" ]; then
        echo "Failed to start Flume agent. Examine the logs at $FLUME_HOME/logs/flume.log for more information."
        return 1
    else
        # Make sure the agent is actually running
        echo "started ($PIDS)"
        echo $PIDS > $PID_FILE
        touch $LOCK_FILE
        agentRunning
        RUNNING=$?
        if [ $RUNNING -gt 0 ]; then
            echo "Although the process was successfully invoked, the agent failed to start properly (status code $RUNNING). Examine the logs at $FLUME_HOME/logs/flume.log for more information."
            return 1
        fi
        return 0
    fi
}

stop() {
    echo "Stopping $SERVICE_NAME: "
    RETVAL=0
    agentRunning
    RUNNING=$?
    if [ $RUNNING -eq 0 ]; then
        PID=`head -1 $PID_FILE`
        kill $PID
        RETVAL=$?
        # Wait for either the process to end or for it to be a zombie process
        while [[ $RUNNING -eq 0 ]]; do
            sleep 1
            agentRunning
            RUNNING=$?
        done
        echo "Stopped PID $PID"
    else
        echo "There is no known service to stop."
    fi
    rm -f $LOCK_FILE
    echo "Service $SERVICE_NAME stopped."
    return $RETVAL
}

status() {
    agentRunning
    RUNNING=$?
    if [ $RUNNING -eq 0 ]; then
        echo "Agent is running"
    else
        echo "Agent is not running or cannot be determined to be running (status code: $RUNNING)"
    fi

    return $RUNNING
}

# Check to see if the agent is running
#
# Returns (per http://refspecs.linuxbase.org/LSB_3.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html):
# - 0: the service is running and OK
# - 1: the service is not running, but the /var/run PID file exists
# - 3: the service is not running
# - 4: the status of the service cannot be determined
agentRunning() {
    if [ ! -e $PID_FILE ]; then
        echo "The PID file $PID_FILE was not found; the agent is assumed to not be running"
        return 3
    fi
    PID=`head -1 $PID_FILE`
    if [[ -d /proc/$PID ]]; then
        if [[ -n `grep zombie /proc/$PID/status` ]]; then
            echo "Warning! A zombie process was detected for $PID"
        fi
        return 0
    else
        # It's possible that the Flume agent was started outside of this script, which means that the agent cannot be started
        pids=()
        for pid in `$PGREP_COMMAND`
        do
            if [[ -d proc/$pid ]]; then
                echo "PID $pid was found to be running"
                pids+=("$pid")
            else
                echo "PID $pid was found in the PID file, but is not running"
            fi
        done
        if [ ${#pids[@]} -ne 1 ]; then
            echo "An instance of the Flume agent (see: $PID_FILE) that is not being tracked by this script was found."
            return 4
        else
            echo "Multiple PIDs were found in the $PID_FILE, but only one (${pids[0]}) was found to still be running"
            echo ${pids[0]} > $PID_FILE
            return 0
        fi

        # The /var/run file exists, but the process isn't actually running
        return 3
    fi
}

case "$1" in
    start)
        start
        ;;
    stop)
{'fraction_non_alphanumeric': 0.10597347729139772, 'fraction_numerical': 0.008449712475061613, 'mean_word_length': 2.3328118889323424, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4552647', 'n_tokens_mistral': 2778, 'n_tokens_neox': 2653, 'n_words': 978}