Über-simple generic RHEL/CentOS init script

Fill in the indicated bits, drop in /etc/rc.d/init.d/ , chmod +x, and away you go!

#!/bin/bash
# chkconfig: 2345 90 90
# description: program_name
### BEGIN INIT INFO
# Provides: program_name
# Required-Start: network
# Required-Stop: network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Description: Start the program
### END INIT INFO

### Fill in these bits:
START_CMD="java -jar /home/kale/bin/program_name.jar > /var/log/program_name/program_name.log 2>&1 &"
NAME="program_name"
PGREP_STRING="/home/kale/bin/program_name.jar"
PID_FILE="/var/run/program_name/program_name.pid"
USER="username"

### No further muckin' about needed!

CUR_USER=`whoami`

killproc() {
  pkill -u $USER -f $PGREP_STRING
}

start_daemon() {
  eval "$*"
}

log_success_msg() {
  echo "$*"
  logger "$_"
}

log_failure_msg() {
  echo "$*"
  logger "$_"
}

check_proc() {
  pgrep -u $USER -f $PGREP_STRING >/dev/null
}

start_script() {
  if [ "${CUR_USER}" != "root" ] ; then
    log_failure_msg "$NAME can only be started as 'root'."
    exit -1
  fi

  check_proc
  if [ $? -eq 0 ]; then
    log_success_msg "$NAME is already running."
    exit 0
  fi

  [ -d /var/run/$NAME ] || (mkdir /var/run/$NAME )

   # make go now 
    start_daemon /bin/su $USER -c "$START_CMD"

  # Sleep for a while to see if anything cries
  sleep 5
  check_proc

  if [ $? -eq 0 ]; then
    log_success_msg "Started $NAME."
  else
    log_failure_msg "Error starting $NAME."
    exit -1
  fi
}

stop_script() {
  if [ "${CUR_USER}" != "root" ] ; then
    log_failure_msg "You do not have permission to stop $NAME."
    exit -1
  fi

  check_proc
  if [ $? -eq 0 ]; then
    killproc -p $PID_FILE >/dev/null

    # Make sure it's dead before we return
    until [ $? -ne 0 ]; do
      sleep 1
      check_proc
    done

    check_proc
    if [ $? -eq 0 ]; then
      log_failure_msg "Error stopping $NAME."
      exit -1
    else
      log_success_msg "Stopped $NAME."
    fi
  else
    log_failure_msg "$NAME is not running or you don't have permission to stop it"
  fi
}

check_status() {
  check_proc
  if [ $? -eq 0 ]; then
    log_success_msg "$NAME is running."
  else
    log_failure_msg "$NAME is stopped."
    exit -1
  fi
}

case "$1" in
  start)
    start_script
    ;;
  stop)
    stop_script
    ;;
  restart)
    stop_script
    start_script
    ;;
  status)
    check_status
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|status}"
    exit 1
esac

exit 0

6 Comments

  • Brett Randall

    April 9, 2013

    PGREP_STRING works better if it is quoted in both references, like this:

    pkill -u $USER -f “${PGREP_STRING}”
    pgrep -u $USER -f “${PGREP_STRING}” >/dev/null

    Does PID_FILE do anything? It looks like it is passed to killproc() function, but not used.

  • Bar Hofesh

    August 12, 2013

    Hi great template :) I only have two questions

    1) where is “$NAME” coming from ?
    2) wont this:
    check_proc
    if [ $? -eq 0 ]; then
    log_success_msg “$NAME is already running.”
    exit 0
    fi

    always returns null ? even if the process isn’t running ?

  • Bar Hofesh

    August 12, 2013

    nm about #1 apparently I mistakenly deleted it…now I see it in your code.

  • Bar Hofesh

    August 12, 2013

    (I would edit but no option too so sorry for multi-posting)
    about #2 I mean return 0 (not null) because the call for “check_proc” is always successful.

  • Thinus

    September 5, 2013

    At least on my system, I had to encapsulate the START_CMD in escaped quotes, as the eval command did not understand the scope, e.g.:
    start_daemon /bin/su $USER -c \”$START_CMD\”

    Without these, /bin/su tried to understand the components of the start-command (i.e. “java”, “-jar”, etc.) as command-line argument for /bin/su, instead of it all being one long command.

  • Dmitriy

    July 15, 2015

    +1 to what Thinus mentioned, I had to encapsulate the command into quotes to get su to get it to execute properly.

    start_daemon /bin/su $USER -c “‘$START_CMD'”

Leave a Reply