Hi ppl, I created a daemon to auto start or autorestart my server it works with the start stop status params. You will need to files:
/admin/updatepid.sh:
Now the /admin/autorestarter.sh:
And now the main file
/etc/init.d/ot.sh:
Now, lets set it in the init scripts
CentOS:
Debian:
Now you can use any of these:
Hope it works for you guys!!!
UPDATE: Updated a litte since it didn't worked in my CentOS 5.5 XD. Now its working!!!!
/admin/updatepid.sh:
Code:
#!/bin/bash
#The path where to save the file, should be the same value in both scripts
OT_PID_FILE="/admin/yourserver.pid"
#Let's sleep the job to let the Server Starts, my server takes around 15 seconds, but I'm setting 40 seconds just in case
sleep 40
#Write The process id to the pid file
pgrep -f autorestarter.sh > ${OT_PID_FILE}
sleep 1
pgrep -P `cat ${OT_PID_FILE}` >> ${OT_PID_FILE}
Now the /admin/autorestarter.sh:
Code:
#!/bin/bash
#Pretty Simple stuff
while [ -e $1 ]
do
$2 > $3 2>&1
done
And now the main file
/etc/init.d/ot.sh:
Code:
#!/bin/bash
#chkconfig: 345 20 80
#description: Maven Continuum server
if [ -z "$1" ]; then
echo "Please provide daemon command to run: start, stop, restart, status"
exit 1
fi
OT_DIR="/yourserver"
OT_BINARY="./TheForgottenServer"
OT_NAME="yourserver OT"
OT_LOG="/yourserver/logs.log"
OT_PID_FILE="/admin/yourserver.pid"
OT_GET_PID_JOB="/admin/updatepid.sh"
OT_AUTORESTARTER="/admin/autorestarter.sh"
function start(){
echo "Starting ${OT_NAME}..."
touch ${OT_PID_FILE}
cd $OT_DIR
ulimit -c unlimited #To be able to debug when crashes
sh ${OT_GET_PID_JOB} &
sh ${OT_AUTORESTARTER} ${OT_PID_FILE} ${OT_BINARY} ${OT_LOG} &
echo "${OT_NAME}... Started"
}
function stop(){
if [ ! -e ${OT_PID_FILE} ]
then
echo "${OT_NAME} not Running"
exit
fi
echo "Stopping ${OT_NAME}..."
parentID=`head -1 ${OT_PID_FILE}`
childID=`tail -1 ${OT_PID_FILE}`
rm -f ${OT_PID_FILE}
kill $parentID
sleep 2
kill $childID
echo "Stopped."
}
function restart(){
stop
start
}
function status(){
echo "Gathering ${OT_NAME} status..."
if [ -f ${OT_PID_FILE} ]; then
pid=`cat ${OT_PID_FILE}`
processes=`ps -no-headers --pid $pid`
if [ "$?" -ne "0" ]; then
echo "${OT_NAME} process is not found, possible crash"
else
echo "${OT_NAME} is running:"
echo "$processes"
tail ${OT_LOG}
fi
else
echo "${OT_NAME} is not started"
fi
echo "Done."
}
$1
Now, lets set it in the init scripts
CentOS:
Code:
chkconfig --add ot.sh
Debian:
Code:
update-rc.d ot.sh defaults
Now you can use any of these:
Code:
/etc/init.d/ot.sh start
/etc/init.d/ot.sh stop
/etc/init.d/ot.sh restart
Hope it works for you guys!!!
UPDATE: Updated a litte since it didn't worked in my CentOS 5.5 XD. Now its working!!!!
Last edited: