not to run duplicate instances of service - linux

I am writing a init.d script for kibana
as of not script is running partially, but the issue is if I run run service kibana start even if service is running then second instance start which bothers me I want to add check before starting service, if service is running then dont start second instance. I tried to put if check on "/var/lock/subsys/kibana" but didn't work. Here is my script :
#!/bin/bash
KIBANA_PATH="/opt/kibana4"
DESC="Kibana Daemon"
NAME=kibana
DAEMON=bin/kibana
CONFIG_DIR=$KIBANA_PATH/config/kibana.yml
LOGFILE=/var/log/kibana/kibana.log
#ARGS="agent --config ${CONFIG_DIR} --log ${LOGFILE}"
SCRIPTNAME=/etc/init.d/kibana
PIDFILE=/var/run/kibana.pid
base=kibana
# Exit if the package is not installed
if [ ! -x "$KIBANA_PATH/$DAEMON" ]; then
{
echo "Couldn't find $DAEMON"
exit 99
}
fi
. /etc/init.d/functions
#
# Function that starts the daemon/service
#
do_start()
{
cd $KIBANA_PATH && \
($DAEMON >> $LOGFILE &) && \
success || failure;
}
set_pidfile()
{
pgrep -f "kibana.jar" > $PIDFILE
}
#
# Function that stops the daemon/service
#
do_stop()
{
pid=`cat $PIDFILE`
if checkpid $pid 2>&1; then
# TERM first, then KILL if not dead
kill -TERM $pid >/dev/null 2>&1
usleep 100000
if checkpid $pid && sleep 1 &&
checkpid $pid && sleep $delay &&
checkpid $pid ; then
kill -KILL $pid >/dev/null 2>&1
usleep 100000
fi
fi
checkpid $pid
RC=$?
[ "$RC" -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown"
}
case "$1" in
start)
echo -n "Starting $DESC: "
do_start
touch /var/lock/subsys/$NAME
set_pidfile
;;
stop)
echo -n "Stopping $DESC: "
do_stop
rm /var/lock/subsys/$NAME
rm $PIDFILE
;;
restart|reload)
echo -n "Restarting $DESC: "
do_stop
do_start
touch /var/lock/subsys/$NAME
set_pidfile
;;
status)
echo $DESC
status -p $PIDFILE
echo $!
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2
exit 3
;;
esac
echo
exit 0
any help here ?
Thanks

use lockfile -r0 /path/to/lock/file.lck when you start the service. every new access then will retry zero times to create the file. so if that command fails do nothing or start the service otherwise.
lockfile -r0 /path/to/lock/file.lck
if [ "$?" == "0" ]; then
echo "lock does not exist. enter devils land :)"
fi

The following is a pretty standard implementation of this feature used by most init.d scripts.
start () {
[ -d /var/run/nscd ] || mkdir /var/run/nscd
[ -d /var/db/nscd ] || mkdir /var/db/nscd
echo -n $"Starting $prog: "
daemon /usr/sbin/nscd $NSCD_OPTIONS
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/nscd
return $RETVAL
}
...
# See how we were called.
case "$1" in
start)
[ -e /var/lock/subsys/nscd ] || start
RETVAL=$?
;;
...

Related

initscript CentOS launching chroot nginx problems

I found this initscript for launching nginx here: http://www.rackspace.com/knowledge_center/article/centos-adding-an-nginx-init-script. Im trying to modify it to work with a chroot environment and its proving more troublesome than I expected.
Here's what I have:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/local/nginx/sbin/nginx"
prog="nginx"
chroot="/usr/sbin/chroot /nginx
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $chroot $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killall $prog
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killall $prog -HUP
retval=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
if rh_status_q; then
printf "$prog already running\n"
exit 0
fi
$1
;;
stop)
if ! rh_status_q; then
printf "$prog not running\n"
exit 0
fi
$1
;;
restart|configtest)
$1
;;
reload)
if ! rh_status_q; then
printf "$prog not running\n"
exit 7
fi
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
if ! rh_status_q; then
printf "$prog not running\n"
exit 0
fi
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
The main thing not working right now is the status call.
[root#localhost ~]# /etc/init.d/nginx status
nginx dead but subsys locked
This error points to a lockfile being present but no pid file. This is true, however when I use the original nginx init script and launch it in a non-chroot environment, there is no pidfile, and the status call works just fine.
I put in some lines to retrieve the pid by launching the process in the background instead of using daemon:
PID=$chroot $nginx -c $NGINX_CONF_FILE > /dev/null 2>&1 $ echo $!
echo $PID > $pidfile
But this grabs the pid of chroot, which ends after nginx is launched. Nginx has both a master and a child pid. Might grabbing those and putting them to a pidfile be the way forward? Or is there something else I should try?
I think its because daemon launches a chroot process it gets the wrong pid.
Alright it was as easy as grepping for the pid
pidfile='/var/run/nginx.pid'
start(){
...
daemon $chroot $nginx -c $NGINX_CONF_FILE
PID=`ps -ef | grep $nginx | grep -v grep | awk '{print $2}'`
echo $PID > $pidfile
...
}
stop() {
...
[ $retval -eq 0 ] && rm -f $lockfile && rm -f $pidfile
return $retval
}

Service status not working

I have the following code for a service that I'm trying to have automatically start on boot.
#!/bin/sh
# Source function library.
. /etc/rc.d/init.d/functions
RETVAL=0
prog='foo'
exec="/usr/sbin/$prog"
pidfile="/var/run/$prog.pid"
lock_file="/var/lock/subsys/$prog"
logfile="/var/log/$prog"
if [ -f /etc/default/foo ]; then
. /etc/default/foo
fi
if [ -z $QUEUE_TYPE ]; then
echo 'ENV variable QUEUE_TYPE has not been set, please set it in /etc/default/foo'
exit 1
fi
get_pid() {
cat "$pidfile"
}
is_running() {
[ -f "$pidfile" ] && ps `get_pid` > /dev/null 2>&1
}
case "$1" in
start)
echo -n "Starting Consul daemon: "
#
daemon --pidfile $pidfile --check foo --user my-user "my app stuff here"
echo
;;
stop)
echo -n 'Stopping Consul daemon: '
killproc foo
echo
;;
status)
status $pidfile
RETVAL=$?
#status -p $pidfile -l $prog
#[ $RETVAL -eq 0 ] && RETVAL=$?
#RETVAL=$?
#if is_running; then
# echo 'Running'
#else
# echo 'Not Running'
#fi
#status foo
#RETVAL=$?
;;
restart)
$0 stop
$0 start
RETVAL=$?
;;
*)
echo 'Usage: foo {start|stop|status|restart}'
exit 1
esac
exit $RETVAL
When I run sudo service foo status it says that it hasn't been started which is correct. After running sudo service foo start and then running the status command, it tells me that the service hasn't been started. I'm not sure what is causing this to happen. I looked at the configurations for other init.d scripts to see how they were handling this and tried to follow their lead. Is there something obvious here that I'm doing wrong or something else that I may be unaware of that's causing this problem?

How Kill node process on killing forever process

I have a script to start and stop my node.js server.
When I stop the script, the forever process is killed however the node process is not terminated.
Is there any way to stop both forver and node process when I issue
Kill $FOREVER_PID
Here is the script -
#!/bin/bash
path="/Users/aayush/Desktop/node/rest-api"
action="forever errorLog_express.js "
logFile="$path/system.log"
pidFile="$path/pidFile.pid"
#messages
usage="Usage : node-script.sh start|stop"
panic="Panic! nothing to do, exiting"
unknown="Unrecognized parameter"
start="[starting node-forever]"
end="[stopping node-forever]"
notRunning="Process node-forever not running"
alreadyRunning="Process node-forever already running"
if [ -z $1 ]
then
echo $panic
echo $usage
exit 0;
fi
if [ $1 = "start" ]
then
# starting process
dummy="OK"
if [ -f $pidFile ];
then
exit 0
else
cd $path
echo "cd $path"
echo $start
echo $start >> $logFile
$action > /dev/null 2>&1 &
Process_Pid=$!
echo $Process_Pid > $pidFile
echo $dummy
exit 0
fi
elif [ $1 = "stop" ]
then
# stopping process by getting pid from pid file
dummy="OK"
echo $end
echo $end >> $logFile
if [ -f $pidFile ];
then
while IFS=: read -r pid
do
# reading line in variable pid
if [ -z $pid ]
then
dummy="FAILED"
echo "Could not parse pid PANIC ! do 'ps' and check manully"
else
echo "Process Pid : $pid"
kill $pid
fi
done <"$pidFile"
rm $pidFile
echo $dummy
exit 0
else
echo $notRunning
echo "FAILED"
exit 0
fi
else
echo $unknown
echo $usage
exit 0
fi
The final script working for me -
#!/bin/bash
#proccessname: node
USER=node
PWD=node
node=node
forever=forever
path="/Users/aayush/Desktop/node/rest-api"
action="forever start -l forever.log -a -o out.log -e err.log errorLog_express.js "
start(){
cd $path
$action
}
stop(){
/usr/local/bin/forever stopall
}
restart(){
stop
start
}
status(){
/usr/local/bin/forever list
}
#Options
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo $ "usage $0 {start | stop | status | restart}"
exit 1
esac
exit 0
Yes there is, use a signal handler in your script to catch the sigterm and kill the node process.
www.gnu.org/software/bash/manual/html_node/Signals.html
$ killall node
Will kill them.

Restart Node.js App with init.d

I'd like to have an init.d daemon restart my node.js app if it crashes. This script starts/stops my node app. I've had no luck getting it to restart the app if it crashes.
I'm running under CentOS. What am I missing?
#!/bin/sh
. /etc/rc.d/init.d/functions
USER="rmlxadmin"
DAEMON="/usr/bin/nodejs"
ROOT_DIR="/home/rmlxadmin"
SERVER="$ROOT_DIR/my_node_app.js"
LOG_FILE="$ROOT_DIR/app.js.log"
LOCK_FILE="/var/lock/subsys/node-server"
do_start()
{
if [ ! -f "$LOCK_FILE" ] ; then
echo -n $"Starting $SERVER: "
runuser -l "$USER" -c "$DAEMON $SERVER >> $LOG_FILE &" && echo_success || echo_failure
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $LOCK_FILE
else
echo "$SERVER is locked."
RETVAL=1
fi
}
do_stop()
{
echo -n $"Stopping $SERVER: "
pid=`ps -aefw | grep "$DAEMON $SERVER" | grep -v " grep " | awk '{print $2}'`
kill -9 $pid > /dev/null 2>&1 && echo_success || echo_failure
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $LOCK_FILE
}
case "$1" in
start)
do_start
;;
stop)
do_stop
;;
restart)
do_stop
do_start
;;
*)
echo "Usage: $0 {start|stop|restart}"
RETVAL=1
esac
exit $RETVAL
You need to use additional tools like node-supervisor for this case.
Install node-supervisor with npm:
sudo npm install -g supervisor
Change DAEMON variable in your init.d script to node-supervisor executable: /usr/bin/supervisor. You can check this path using command 'whereis supervisor' in your system (after installation, of course).
Now supervisor will restart your application if it's crash.

Grace period in bash init.d script before killing process when calling '/etc/init.d/daemon stop'

I'm modifying an init.d script for one of my company apps. Apparently my bash script foo is not strong enough.
Once the Launcher has seen the Dispatcher has terminated, it will exit. The vanilla script always displays [ FAILED ] when stopping the app as the Launcher has terminated before this script tries to kill it.
I've tried to add in a 2 second grace period (plenty) for the Launcher to terminate before attempting to kill it if it's still running.
I have the following stop() function:
#!/bin/bash
# chkconfig: 345 85 60
# description: .
# processname: xxx
# pidfile: /var/run/xxx.pid
# Source function library.
. /etc/rc.d/init.d/functions
RETVAL=0
PID_PATH=/tmp/
PID_PREFIX=xxx
LOCK_PATH=/tmp/
PIDFILE_D=${PID_PATH}${PID_PREFIX}_dispatcher.pid
PIDFILE_L=${PID_PATH}${PID_PREFIX}_launcher.pid
PIDFILE_J=${PID_PATH}${PID_PREFIX}_jobselector.pid
LOCK=${LOCK_PATH}${PID_PREFIX}
BASEPATH=`dirname $0`
echo $BASEPATH | grep -q "^/" && BASEPATH=`dirname $BASEPATH` || BASEPATH=$PWD/`dirname $BASEPATH`
# -- snip ---
stop() {
echo -n $"Shutting down Dispatcher: "
killproc -p $PIDFILE_D
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $PIDFILE_D
echo -n $"Shutting down Launcher: "
# launcher self terminates once it sees the dispatcher is not running
# grace period of 2 seconds before explicitly killing it
if [ -e "$PIDFILE_L" ]; then
local i pid
read pid < "$PIDFILE_L"
for i in 1 2; do
if checkpid $pid 2>&1; then
sleep 1
fi
done
# if launcher still active after grace period, kill it
if checkpid $pid 2>&1; then
killproc -p $PIDFILE_L
RETVAL=$?
else
success $"$base shutdown"
fi
else
success $"$base shutdown"
fi
[ $RETVAL -eq 0 ] && rm -f $PIDFILE_L
echo -n $"Shutting down Job Selector: "
killproc -p $PIDFILE_J
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $PIDFILE_J
[ $RETVAL -eq 0 ] && rm -f $LOCK
return $RETVAL
}
When I stop the program, it doesn't display the "Shutting down Launcher: ... " line anymore.
$ sudo ./bin/program stop
Shutting down Dispatcher: [ OK ]
Shutting down Job Selector: [ OK ]
Supporting functions:
killproc() {
local RC killlevel= base pid pid_file= delay
RC=0; delay=3
# Test syntax.
if [ "$#" -eq 0 ]; then
echo $"Usage: killproc [-p pidfile] [ -d delay] {program} [-signal]"
return 1
fi
if [ "$1" = "-p" ]; then
pid_file=$2
shift 2
fi
if [ "$1" = "-d" ]; then
delay=$2
shift 2
fi
# check for second arg to be kill level
[ -n "${2:-}" ] && killlevel=$2
# Save basename.
base=${1##*/}
# Find pid.
__pids_var_run "$1" "$pid_file"
if [ -z "$pid_file" -a -z "$pid" ]; then
pid="$(__pids_pidof "$1")"
fi
# Kill it.
if [ -n "$pid" ] ; then
[ "$BOOTUP" = "verbose" -a -z "${LSB:-}" ] && echo -n "$base "
if [ -z "$killlevel" ] ; then
if checkpid $pid 2>&1; then
# TERM first, then KILL if not dead
kill -TERM $pid >/dev/null 2>&1
usleep 100000
if checkpid $pid && sleep 1 &&
checkpid $pid && sleep $delay &&
checkpid $pid ; then
kill -KILL $pid >/dev/null 2>&1
usleep 100000
fi
fi
checkpid $pid
RC=$?
[ "$RC" -eq 0 ] && failure $"$base shutdown" || success $"$base shutdown"
RC=$((! $RC))
# use specified level only
else
if checkpid $pid; then
kill $killlevel $pid >/dev/null 2>&1
RC=$?
[ "$RC" -eq 0 ] && success $"$base $killlevel" || failure $"$base $killlevel"
elif [ -n "${LSB:-}" ]; then
RC=7 # Program is not running
fi
fi
else
if [ -n "${LSB:-}" -a -n "$killlevel" ]; then
RC=7 # Program is not running
else
failure $"$base shutdown"
RC=0
fi
fi
# Remove pid file if any.
if [ -z "$killlevel" ]; then
rm -f "${pid_file:-/var/run/$base.pid}"
fi
return $RC
}
# Check if $pid (could be plural) are running
checkpid() {
local i
for i in $* ; do
[ -d "/proc/$i" ] && return 0
done
return 1
}
# Log that something succeeded
success() {
#if [ -z "${IN_INITLOG:-}" ]; then
# initlog $INITLOG_ARGS -n $0 -s "$1" -e 1
#fi
[ "$BOOTUP" != "verbose" -a -z "${LSB:-}" ] && echo_success
return 0
}
echo_success() {
[ "$BOOTUP" = "color" ] && $MOVE_TO_COL
echo -n "["
[ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
echo -n $" OK "
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo -n "]"
echo -ne "\r"
return 0
}
What is the relevance of $ is in the echo line?
echo -n $"Shutting down Launcher: "
If I add in the debug line:
echo "RETVAL=$RETVAL"
[ $RETVAL -eq 0 ] && rm -f $PIDFILE_L
I get:
Shutting down Dispatcher: [FAILED]
RETVAL=0 down Launcher: [ OK ]
Shutting down Job Selector: [FAILED]
Any bash script gurus out there?
Turns out all I needed was an echo statement before the following line.
echo
[ $RETVAL -eq 0 ] && rm -f $PIDFILE_L
This is related to my question about the behaviour of echo -n $"Shutting down Launcher".
The echo -n switch supresses the line feed character \n. The $ preceding the quotes outputs carriage return \r to move the cursor back to the beginning of the line.
Without the echo statement, the Job Selector shutdown line was overwriting the Launcher shutdown line.

Resources