4

This is my bash script. All it does is to check whether a service has started and whether some processes are running as expected.

It ends abruptly with a "Terminated" message. I tried debugging it with the set -x flag, and I still don't know what is going wrong. Stack Overflow and Google don't show me any other people with a similar problem.

The script has permissions of 755. Some commands are obfuscated, for obvious reasons.

#!/bin/bash

set -x 

DAEMON_BIN=/etc/init.d/init-god
DAEMON_BIN_START="${DAEMON_BIN} start"
DAEMON_BIN_STOP="${DAEMON_BIN} stop"
SOME_VARIABLE="foo"

CHEF_CONFIG_FILE_PATH="/path/to/file"
NODE_INFO_FILE="/mnt/node_info/properties"

function get_key_value (){
    value=$(grep -Po "(?<=^${1}:).*" ${NODE_INFO_FILE})
    echo $value;
}

eval $DAEMON_BIN_STOP

nohup pkill DAEMON &> /dev/null

nohup pkill -f resque &> /dev/null

eval $DAEMON_BIN_START

sleep 15

PROCESS_COUNT=`ps aux | awk '/[p]rocess-name/' | wc -l`

NODE_NAME=`get_key_value node_name`

if [[ $NODE_NAME -eq $SOME_VARIABLE  && $PROCESS_COUNT -eq 1 ]]; then 
    echo "DAEMON and scheduler are running fine." ;
else
    echo "A problem with DAEMON has occured." ;
fi

EXPECTED_PROCESS_COUNT=`get_key_value no_of_workers`
ACTUAL_WORKER_COUNT=`ps ax | grep [r]esque-[0-9]`

if [[ $EXPECTED_PROCESS_COUNT -eq $ACTUAL_WORKER_COUNT ]]; then 
    echo "Correct Number of workers initialized." ;
else
    echo "More workers exist than are permitted." ;
fi

for (( i=0; i<${EXPECTED_PROCESS_COUNT}; i++ )); do
    WORKER_NAME=`get_key_value worker_${i}`
    COUNT=`ps ax | grep ${WORKER_NAME} | grep -v grep | wc -l`
    if [[ $COUNT -eq 1 ]]; then
        #statements
        echo "${WORKER_NAME} is running."
    else
        echo "${WORKER_NAME} may not be running or might have more than 1 copies."
    fi
done

The debug output of the script is as follows:

+ DAEMON_BIN=/etc/init.d/init-god
+ DAEMON_BIN_START='/etc/init.d/init-god start'
+ DAEMON_BIN_STOP='/etc/init.d/init-god stop'
+ SOME_VARIABLE=foo
+ CHEF_CONFIG_FILE_PATH=/path/to/file
+ NODE_INFO_FILE=/mnt/node_info/properties
+ eval /etc/init.d/init-god stop
++ /etc/init.d/init-god stop
.
Stopped all watches
Stopped god
+ nohup pkill DAEMON
+ nohup pkill -f resque
Terminated

Why does this script exit with a "Terminated" message? What should I do to prevent this from happening?

  • 1
    I provided an answer to the question, but I also want to note that the script is broken by design. See: mywiki.wooledge.org/ProcessManagement – jordanm Oct 9 '13 at 5:39
  • Thank you. I am taking a look at it, especially the process killing part. – theTuxRacer Oct 9 '13 at 5:48
  • Please provide the name of the script when you are using ps :-) – ChuckCottrill Oct 9 '13 at 6:20
12

When you are calling pkill -f resque it is also matching your script, sending it a SIGTERM. If you are unable to add additional restrictions on the pkill commands such as more exact matching, you will need to kill PIDs one at a time to ensure the script is not killing itself. Here is an example:

pids=( $(pgrep -f resque) )
for pid in "${pids[@]}"; do
  if [[ $pid != $$ ]]; then
    kill "$pid"
  fi
done
  • 1
    mypid=$$; pids=($(pgrep -f resque|grep -v $mypid)) – ChuckCottrill Oct 9 '13 at 6:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.