# Custom rc startup script - What's wrong?



## Ruler2112 (May 31, 2011)

I have a local FreeBSD 8.0 server running ejabberd to provide instant messaging services to the machines on my local network.  Bandersnatch is a piece of software that will log all IMs sent/received by the various people in the store to a mysql database.

The logging features of it are all designed to be spit out to the console, so I redirect them into a log file.  Because of this fact and that the software doesn't respond to the signals sent by newsyslog, I wrote an indirection program that will kill/restart bandersnatch when it receives the signal from newsyslogd, effectively making the daemon respond to the request to rotate the log files.

I wrote a startup script and put it in /usr/local/etc/rc.d, flagged it as executable, and put bandersnatch_enable in /etc/rc.conf.  It works perfectly when run as root, but does not run automatically when the machine boots.  Nothing is logged until I log in and manually run the startup script as root, plus then the restart status messages are echoed to my login.

After getting extremely frustrated, I directly copied one of the standard startup scripts and modified it to simply execute the commands needed by bandersnatch and do a search/replace on the original daemon name for everything else, but _still_ can't get it to start when the machine does.  This has been this way for almost a year and I've just lived with it, tweaking little things in the hopes that it'll fix the problem without success, and have finally decided that enough is enough - it's gotta get fixed.  (Doesn't help that we have extended power outages on average of about once/week.  )

I've included some information below - can anybody see where I've gone wrong?


```
server# cat /etc/rc.conf |grep bandersnatch
bandersnatch_enable="YES"
server# ls -l /usr/local/etc/rc.d/bandersnatch
-r-xr-xr-x  1 root  wheel  3455 May 24 12:01 /usr/local/etc/rc.d/bandersnatch
server# cat /usr/local/etc/rc.d/bandersnatch
#!/usr/local/bin/bash
#
# Add the following lines to /etc/rc.conf to enable bandersnatch:
# bandersnatch_enable (bool):   Set it to "YES" to enable bandersnatch.
#                               Default is "NO".
# bandersnatch_logfile (path):  Set full path to bandersnatch.log.
#                               Default is "/var/log/bandersnatch.log".
# bandersnatch_config (path):   Set full path to configuration XML file.
#                               Default is "/usr/local/etc/bandersnatch.conf".
#

. /etc/rc.subr

name="bandersnatch"
rcvar=${name}_enable

load_rc_config $name

: ${bandersnatch_enable:="NO"}
: ${bandersnatch_config:="/usr/local/etc/bandersnatch.conf"}
: ${bandersnatch_logfile:="/var/log/bandersnatch.log"}

start_cmd=${name}_start
stop_cmd=${name}_stop

configfile=${bandersnatch_config}
logfile=${bandersnatch_logfile}

bandersnatch_start() {
  runningpid=`/bin/ps -ax | /usr/bin/grep "/usr/bin/perl /usr/local/bandersnatch/bandersnatch" | /usr/bin/grep -v "grep" | /usr/bin/awk '{print $1}'`
  if [ "$runningpid" != "" ] ; then
    /bin/echo "Bandersnatch is already running with PID $runningpid."
  else
    /bin/echo "Starting bandersnatch..."
    /usr/bin/perl /usr/local/bandersnatch/bandersnatch ${configfile} 1>> ${logfile} 2>>${logfile} &
  fi
  indirectpid=`/bin/ps -ax | /usr/bin/grep "/usr/local/sbin/indirect-bandersnatch" | /usr/bin/grep -v "grep" | /usr/bin/awk '{print $1}'`
  if [ "$indirectpid" != "" ] ; then
    /bin/echo "Bandersnatch indirection is already running with PID $indirectpid."
  else
    /bin/echo "Starting bandersnatch indirection..."
    /usr/local/sbin/indirect-bandersnatch &
  fi
}

bandersnatch_stop() {
  runningpid=`/bin/ps -ax | /usr/bin/grep "/usr/bin/perl /usr/local/bandersnatch/bandersnatch" | /usr/bin/grep -v "grep" | /usr/bin/awk '{print $1}'`
  if [ "$runningpid" != "" ] ; then
    /bin/echo "Stopping bandersnatch..."
    /bin/kill -15 `/bin/ps -ax | /usr/bin/grep "/usr/bin/perl /usr/local/bandersnatch/bandersnatch" | /usr/bin/grep -v "grep" | /usr/bin/awk '{print $1}'`
    /bin/sleep 2
    runningpid=`/bin/ps -ax | /usr/bin/grep "/usr/bin/perl /usr/local/bandersnatch/bandersnatch" | /usr/bin/grep -v "grep" | /usr/bin/awk '{print $1}'`
    if [ "$runningpid" != "" ] ; then
      /bin/echo "Bandersnatch not responding - forcing stop."
      /bin/kill -9 `/bin/ps -ax | /usr/bin/grep "/usr/bin/perl /usr/local/bandersnatch/bandersnatch" | /usr/bin/grep -v "grep" | /usr/bin/awk '{print $1}'`
    fi
  else
    /bin/echo "Bandersnatch is not currently running!"
  fi
  indirectpid=`/bin/ps -ax | /usr/bin/grep "/usr/local/sbin/indirect-bander" | /usr/bin/grep -v "grep" | /usr/bin/awk '{print $1}'`
  if [ "$indirectpid" != "" ] ; then
    /bin/echo "Stopping bandersnatch indirection..."
    /bin/kill `/bin/ps -ax | /usr/bin/grep "/usr/local/sbin/indirect-bander" | /usr/bin/grep -v "grep" | /usr/bin/awk '{print $1}'`
    /bin/sleep 10
    indirectpid=`/bin/ps -ax | /usr/bin/grep "/usr/local/sbin/indirect-bander" | /usr/bin/grep -v "grep" | /usr/bin/awk '{print $1}'`
    if [ "$indirectpid" != "" ] ; then
      /bin/echo "Bandersnatch indirection not responding - forcing stop."
      /bin/kill -9 `/bin/ps -ax | /usr/bin/grep "/usr/local/sbin/indirect-bander" | /usr/bin/grep -v "grep" | /usr/bin/awk '{print $1}'`
    fi
  else
    /bin/echo "Bandersnatch indirection is not currently running!"
  fi
}

bandersnatch_restart() {
  run_rc_command "bandersnatch_stop"
  run_rc_command "bandersnatch_start"
}

load_rc_config $name
run_rc_command "$1"
```


----------



## ohauer (May 31, 2011)

A couple of issues and suggestions.

Your script is missing the PROVIDE flags, and please use *#!/bin/sh* in such scripts everything else _(bash)_ is not supported.

To have a good overview take a look into rc(8), rc.subr(8) and http://www.freebsd.org/doc/en_US.ISO8859-1/books/porters-handbook/rc-scripts.html

I would start over with the following snippet (only a starting suggestion)


```
#!/bin/sh
#
[color="Red"]# PROVIDE: bandersnatch
# REQUIRE: DAEMON mysql
# BEFORE: LOGIN
# KEYWORD: shutdown[/color]
#
#
# Add the following lines to /etc/rc.conf to enable bandersnatch:
# bandersnatch_enable (bool):   Set it to "YES" to enable bandersnatch.
...

. /etc/rc.subr

name=bandersnatch
rcvar=`set_rcvar`

command=/usr/local/bandersnatch/bandersnatch

[color="Red"]# Not a binary, then use command_interpreter
command_interpreter=/usr/bin/perl[/color]

: ${bandersnatch_enable:="NO"}
: ${bandersnatch_config:="/usr/local/etc/bandersnatch.conf"}
: ${bandersnatch_logfile:="/var/log/bandersnatch.log"}

start_cmd=${name}_start
stop_cmd=${name}_stop

bandersnatch_start() {
  ${command} ${configfile} 1>> ${logfile} 2>>${logfile} &
}

bandersnatch_stop() {
  # get all pids ...
  pids=`check_process ${command} ${command_interpreter}`
  ...
```


----------



## SirDice (Jun 1, 2011)

> ```
> runningpid=`/bin/ps -ax | /usr/bin/grep "/usr/bin/perl /usr/local/bandersnatch/bandersnatch" | /usr/bin/grep -v "grep" | /usr/bin/awk '{print $1}'`
> ```


:O

This looks a lot simpler, doesn't it?

```
runningpid=`pgrep -f bandersnatch`
```


----------

