Wednesday, May 5, 2010

Add a custom script as a daemon on reboot of a redhat centos server


To enable a custom script in a redhat centos server linux box you have to follow those simple steps.
This is useful when you have to start a server process that don't return anything..
One example is openoffice server aka (soffice).
Suppose you have two files.
The first file A call the second file named B-so
The file A contains a code like this

# Source function library.
. /etc/rc.d/init.d/functions

[ -f /usr/local/sbin/B-so ] || exit 0


prog="B-so"
start() {
echo -n $"Starting $prog: "
daemon /usr/local/sbin/B-so 1> /dev/null 2> /dev/null &
RETVAL=$?
echo
return $RETVAL
}

stop() {
if test "x`ps aux | grep B-so | grep -v grep | awk '{ print $2 }'`" != x; then
echo -n $"Stopping $prog: "
killproc B-so
killall processname
echo
fi
RETVAL=$?
return $RETVAL
}

case "$1" in
start)
start
;;

stop)
stop
;;

status)
status B-so
;;

restart)
stop
start
;;

*)
echo $"Usage: $0 {start|stop|restart|status}"
exit 1

esac

exit $RETVAL


Copy the file A in the directory /etc/init.d/

Copy the second file B.so in the dir /usr/local/sbin/
The file B-so contains a code like this:


. /etc/rc.d/init.d/functions

LOG="/root/mail2fax/log/safe-B.log"


while :
do
...done some stuff ...
done



After that you have to add a line with the path to the service to start in the /etc/rc.local.

Check if permissions on those two files are set on 0755,then you could reboot your server and verify if the service start successfully.


img credits wmliu



Read more...